home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / process.c < prev    next >
C/C++ Source or Header  |  1993-11-22  |  96KB  |  3,349 lines

  1. /* Asynchronous subprocess control for GNU Emacs.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <signal.h>
  22.  
  23. #include "config.h"
  24.  
  25. #ifdef    WINDOWSNT
  26. #define    NOMINMAX
  27. #include <windows.h>
  28. #include "ntsig.h"
  29. #include "ntproc_p.h"
  30. #endif    /* !WINDOWSNT */
  31.  
  32. /* This file is split into two parts by the following preprocessor
  33.    conditional.  The 'then' clause contains all of the support for
  34.    asynchronous subprocesses.  The 'else' clause contains stub
  35.    versions of some of the asynchronous subprocess routines that are
  36.    often called elsewhere in Emacs, so we don't have to #ifdef the
  37.    sections that call them.  */
  38.  
  39.  
  40. #ifdef subprocesses
  41.  
  42. #ifdef STDC_HEADERS
  43. #include <stdlib.h>
  44. #endif
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include <setjmp.h>
  48. #include <sys/types.h>        /* some typedefs are used in sys/file.h */
  49. #ifdef WINDOWSNT
  50. #include <fcntl.h>
  51. #else /* !WINDOWSNT */
  52. #include <sys/file.h>
  53. #endif /* !WINDOWSNT */
  54. #include <sys/stat.h>
  55.  
  56. #ifdef HAVE_SOCKETS    /* TCP connection support, if kernel can do it */
  57. #include <sys/socket.h>
  58. #include <netdb.h>
  59. #include <netinet/in.h>
  60. #include <arpa/inet.h>
  61. #endif /* HAVE_SOCKETS */
  62.  
  63. #if defined(BSD) || defined(STRIDE)
  64. #include <sys/ioctl.h>
  65. #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
  66. #include <fcntl.h>
  67. #endif /* HAVE_PTYS and no O_NDELAY */
  68. #endif /* BSD or STRIDE */
  69.  
  70. #ifdef NEED_BSDTTY
  71. #include <bsdtty.h>
  72. #endif
  73.  
  74. #ifdef IRIS
  75. #include <sys/sysmacros.h>    /* for "minor" */
  76. #endif /* not IRIS */
  77.  
  78. #include "systime.h"
  79. #include "systty.h"
  80.  
  81. #include "lisp.h"
  82. #include "window.h"
  83. #include "buffer.h"
  84. #include "process.h"
  85. #include "termopts.h"
  86. #include "commands.h"
  87. #include "frame.h"
  88. #include "termhooks.h"
  89.  
  90. #ifdef STDC_HEADERS
  91. #include <io.h>
  92. #endif
  93. #include "process_p.h"
  94. #include "insdel_p.h"
  95. #include "alloca_p.h"
  96. #include "sysdep_p.h"
  97. #include "intervals_p.h"
  98. #include "keyboard_d.h"
  99. #include "xdisp_p.h"
  100. #include "dispnew_p.h"
  101. static Lisp_Object start_process_unwind _P_((Lisp_Object proc));
  102. static void process_send_signal _P_((Lisp_Object process, int signo,
  103.                                      Lisp_Object current_group, int nomsg));
  104. static Lisp_Object exec_sentinel_unwind _P_((Lisp_Object data));
  105. static void exec_sentinel _P_((Lisp_Object proc, Lisp_Object reason));
  106.  
  107. Lisp_Object Qrun, Qstop, Qsignal, Qopen, Qclosed;
  108. /* Qexit is declared and initialized in eval.c.  */
  109.  
  110. /* a process object is a network connection when its childp field is neither
  111.    Qt nor Qnil but is instead a string (name of foreign host we
  112.    are connected to + name of port we are connected to) */
  113.  
  114. #ifdef HAVE_SOCKETS
  115. static Lisp_Object stream_process;
  116.  
  117. #define NETCONN_P(p) (XGCTYPE (XPROCESS (p)->childp) == Lisp_String)
  118. #else
  119. #define NETCONN_P(p) 0
  120. #endif /* HAVE_SOCKETS */
  121.  
  122. /* Define first descriptor number available for subprocesses.  */
  123. #ifdef VMS
  124. #define FIRST_PROC_DESC 1
  125. #else /* Not VMS */
  126. #define FIRST_PROC_DESC 3
  127. #endif
  128.  
  129. /* Define SIGCHLD as an alias for SIGCLD.  There are many conditionals
  130.    testing SIGCHLD.  */
  131.  
  132. #if !defined (SIGCHLD) && defined (SIGCLD)
  133. #define SIGCHLD SIGCLD
  134. #endif /* SIGCLD */
  135.  
  136. #include "syssignal.h"
  137.  
  138. /* Define the structure that the wait system call stores.
  139.    On many systems, there is a structure defined for this.
  140.    But on vanilla-ish USG systems there is not.  */
  141.  
  142. #ifndef VMS
  143. #ifndef WAITTYPE
  144. #if (!defined (BSD) && !defined (UNIPLUS) && !defined (STRIDE) && !(defined (HPUX) && !defined (NOMULTIPLEJOBS)) && !defined (HAVE_WAIT_HEADER)) || defined (LINUX)
  145. #define WAITTYPE int
  146. #define WIFSTOPPED(w) ((w&0377) == 0177)
  147. #define WIFSIGNALED(w) ((w&0377) != 0177 && (w&~0377) == 0)
  148. #define WIFEXITED(w) ((w&0377) == 0)
  149. #define WRETCODE(w) (w >> 8)
  150. #define WSTOPSIG(w) (w >> 8)
  151. #define WTERMSIG(w) (w & 0377)
  152. #ifndef WCOREDUMP
  153. #define WCOREDUMP(w) ((w&0200) != 0)
  154. #endif
  155. #else 
  156. #ifdef BSD4_1
  157. #include <wait.h>
  158. #else
  159. #include <sys/wait.h>
  160. #endif /* not BSD 4.1 */
  161.  
  162. #define WAITTYPE union wait
  163. #define WRETCODE(w) w.w_retcode
  164. #define WCOREDUMP(w) w.w_coredump
  165.  
  166. #ifdef HPUX
  167. /* HPUX version 7 has broken definitions of these.  */
  168. #undef WTERMSIG
  169. #undef WSTOPSIG
  170. #undef WIFSTOPPED
  171. #undef WIFSIGNALED
  172. #undef WIFEXITED
  173. #endif
  174.  
  175. #ifndef WTERMSIG
  176. #define WTERMSIG(w) w.w_termsig
  177. #endif
  178. #ifndef WSTOPSIG
  179. #define WSTOPSIG(w) w.w_stopsig
  180. #endif
  181. #ifndef WIFSTOPPED
  182. #define WIFSTOPPED(w) (WTERMSIG (w) == 0177)
  183. #endif
  184. #ifndef WIFSIGNALED
  185. #define WIFSIGNALED(w) (WTERMSIG (w) != 0177 && (WSTOPSIG (w)) == 0)
  186. #endif
  187. #ifndef WIFEXITED
  188. #define WIFEXITED(w) (WTERMSIG (w) == 0)
  189. #endif
  190. #endif /* BSD or UNIPLUS or STRIDE */
  191. #endif /* no WAITTYPE */
  192. #else /* VMS */
  193. #define WAITTYPE int
  194. #define WIFSTOPPED(w) 0
  195. #define WIFSIGNALED(w) 0
  196. #define WIFEXITED(w) ((w) != -1)
  197. #define WRETCODE(w) (w)
  198. #define WSTOPSIG(w) (w)
  199. #define WCOREDUMP(w) 0
  200. #define WTERMSIG(w) (w)
  201. #include <ssdef.h>
  202. #include <iodef.h>
  203. #include <clidef.h>
  204. #include "vmsproc.h"
  205. #endif /* VMS */
  206.  
  207. /* Needs WAITTYPE */
  208. static Lisp_Object status_convert _P_((WAITTYPE w));
  209.  
  210. #ifndef STDC_HEADERS
  211. extern int errno;
  212. extern char *sys_errlist[];
  213. extern int sys_nerr;
  214. #endif
  215.  
  216. #ifndef VMS
  217. #ifndef BSD4_1
  218. #ifndef WINDOWSNT
  219. #ifndef LINUX
  220. extern char *sys_siglist[];
  221. #endif
  222. #else
  223. char *sys_siglist[] =
  224.   {
  225.     "bum signal!!",
  226.     "hangup",
  227.     "interrupt",
  228.     "quit",
  229.     "illegal instruction",
  230.     "trace trap",
  231.     "iot instruction",
  232.     "emt instruction",
  233.     "floating point exception",
  234.     "kill",
  235.     "bus error",
  236.     "segmentation violation",
  237.     "bad argument to system call",
  238.     "write on a pipe with no one to read it",
  239.     "alarm clock",
  240.     "software termination signal from kill",
  241.     "status signal",
  242.     "sendable stop signal not from tty",
  243.     "stop signal from tty",
  244.     "continue a stopped process",
  245.     "child status has changed",
  246.     "background read attempted from control tty",
  247.     "background write attempted from control tty",
  248.     "input record available at control tty",
  249.     "exceeded CPU time limit",
  250.     "exceeded file size limit"
  251.     };
  252. #endif /* !WINDOWSNT */
  253. #endif
  254. #endif /* VMS */
  255.  
  256. /* t means use pty, nil means use a pipe,
  257.    maybe other values to come.  */
  258. Lisp_Object Vprocess_connection_type;
  259.  
  260. #ifdef SKTPAIR
  261. #ifndef HAVE_SOCKETS
  262. #include <sys/socket.h>
  263. #endif
  264. #endif /* SKTPAIR */
  265.  
  266. /* Number of events of change of status of a process.  */
  267. int process_tick;
  268.  
  269. /* Number of events for which the user or sentinel has been notified.  */
  270. int update_tick;
  271.  
  272. #include "sysfdset.h"
  273.  
  274. /* Mask of bits indicating the descriptors that we wait for input on */
  275.  
  276. SELECT_TYPE input_wait_mask;
  277.  
  278. int delete_exited_processes;
  279.  
  280. /* Indexed by descriptor, gives the process (if any) for that descriptor */
  281. Lisp_Object chan_process[MAXDESC];
  282.  
  283. /* Alist of elements (NAME . PROCESS) */
  284. Lisp_Object Vprocess_alist;
  285.  
  286. Lisp_Object Qprocessp;
  287.  
  288. /* Buffered-ahead input char from process, indexed by channel.
  289.    -1 means empty (no char is buffered).
  290.    Used on sys V where the only way to tell if there is any
  291.    output from the process is to read at least one char.
  292.    Always -1 on systems that support FIONREAD.  */
  293.  
  294. int proc_buffered_char[MAXDESC];
  295.  
  296. /* Compute the Lisp form of the process status, p->status, from
  297.    the numeric status that was returned by `wait'.  */
  298.  
  299. _VOID_
  300. update_status (p)
  301.      struct Lisp_Process *p;
  302. {
  303.   union { int i; WAITTYPE wt; } u;
  304.   u.i = XFASTINT (p->raw_status_low) + (XFASTINT (p->raw_status_high) << 16);
  305.   p->status = status_convert (u.wt);
  306.   p->raw_status_low = Qnil;
  307.   p->raw_status_high = Qnil;
  308. }
  309.  
  310. /*  Convert a process status word in Unix format to 
  311.     the list that we use internally.  */
  312.  
  313. static Lisp_Object
  314. status_convert (w)
  315.      WAITTYPE w;
  316. {
  317.   if (WIFSTOPPED (w))
  318.     return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
  319.   else if (WIFEXITED (w))
  320.     return Fcons (Qexit, Fcons (make_number (WRETCODE (w)),
  321.                 WCOREDUMP (w) ? Qt : Qnil));
  322.   else if (WIFSIGNALED (w))
  323.     return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)),
  324.                   WCOREDUMP (w) ? Qt : Qnil));
  325.   else
  326.     return Qrun;
  327. }
  328.  
  329. /* Given a status-list, extract the three pieces of information
  330.    and store them individually through the three pointers.  */
  331.  
  332. void
  333. decode_status (l, symbol, code, coredump)
  334.      Lisp_Object l;
  335.      Lisp_Object *symbol;
  336.      int *code;
  337.      int *coredump;
  338. {
  339.   Lisp_Object tem;
  340.  
  341.   if (XTYPE (l) == Lisp_Symbol)
  342.     {
  343.       *symbol = l;
  344.       *code = 0;
  345.       *coredump = 0;
  346.     }
  347.   else
  348.     {
  349.       *symbol = XCONS (l)->car;
  350.       tem = XCONS (l)->cdr;
  351.       *code = XFASTINT (XCONS (tem)->car);
  352.       tem = XCONS (tem)->cdr;
  353.       *coredump = !NILP (tem);
  354.     }
  355. }
  356.  
  357. /* Return a string describing a process status list.  */
  358.  
  359. Lisp_Object 
  360. status_message (status)
  361.      Lisp_Object status;
  362. {
  363.   Lisp_Object symbol;
  364.   int code, coredump;
  365.   Lisp_Object string, string2;
  366.  
  367.   decode_status (status, &symbol, &code, &coredump);
  368.  
  369.   if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
  370.     {
  371. #ifndef VMS
  372.       string = build_string (code < NSIG ? sys_siglist[code] : "unknown");
  373. #else
  374. #ifndef WINDOWSNT
  375.       string = build_string (code < NSIG ? sys_errlist[code] : "unknown");
  376. #else /* !WINDOWSNT */
  377.       string = build_string ("unknown");
  378. #endif /* !WINDOWSNT */
  379. #endif
  380.       string2 = build_string (coredump ? " (core dumped)\n" : "\n");
  381.       XSTRING (string)->data[0] = DOWNCASE (XSTRING (string)->data[0]);
  382.       return concat2 (string, string2);
  383.     }
  384.   else if (EQ (symbol, Qexit))
  385.     {
  386.       if (code == 0)
  387.     return build_string ("finished\n");
  388.       string = Fnumber_to_string (make_number (code));
  389.       string2 = build_string (coredump ? " (core dumped)\n" : "\n");
  390.       return concat2 (build_string ("exited abnormally with code "),
  391.               concat2 (string, string2));
  392.     }
  393.   else
  394.     return Fcopy_sequence (Fsymbol_name (symbol));
  395. }
  396.  
  397. #ifdef HAVE_PTYS
  398.  
  399. /* Open an available pty, returning a file descriptor.
  400.    Return -1 on failure.
  401.    The file name of the terminal corresponding to the pty
  402.    is left in the variable pty_name.  */
  403.  
  404. char pty_name[24];
  405.  
  406. int
  407. allocate_pty ()
  408. {
  409.   struct stat stb;
  410.   register c, i;
  411.   int fd;
  412.  
  413.   /* Some systems name their pseudoterminals so that there are gaps in
  414.      the usual sequence - for example, on HP9000/S700 systems, there
  415.      are no pseudoterminals with names ending in 'f'.  So we wait for
  416.      three failures in a row before deciding that we've reached the
  417.      end of the ptys.  */
  418.   int failed_count = 0;
  419.  
  420. #ifdef PTY_ITERATION
  421.   PTY_ITERATION
  422. #else
  423.   for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
  424.     for (i = 0; i < 16; i++)
  425. #endif
  426.       {
  427. #ifdef PTY_NAME_SPRINTF
  428.     PTY_NAME_SPRINTF
  429. #else
  430.     sprintf (pty_name, "/dev/pty%c%x", c, i);
  431. #endif /* no PTY_NAME_SPRINTF */
  432.  
  433. #ifdef PTY_OPEN
  434.     PTY_OPEN;
  435. #else /* no PTY_OPEN */
  436. #ifdef IRIS
  437.     /* Unusual IRIS code */
  438.      *ptyv = open ("/dev/ptc", O_RDWR | O_NDELAY, 0);
  439.      if (fd < 0)
  440.        return -1;
  441.     if (fstat (fd, &stb) < 0)
  442.       return -1;
  443. #else /* not IRIS */
  444.     if (stat (pty_name, &stb) < 0)
  445.       {
  446.         failed_count++;
  447.         if (failed_count >= 3)
  448.           return -1;
  449.       }
  450.     else
  451.       failed_count = 0;
  452. #ifdef O_NONBLOCK
  453.     fd = open (pty_name, O_RDWR | O_NONBLOCK, 0);
  454. #else
  455.     fd = open (pty_name, O_RDWR | O_NDELAY, 0);
  456. #endif
  457. #endif /* not IRIS */
  458. #endif /* no PTY_OPEN */
  459.  
  460.     if (fd >= 0)
  461.       {
  462.         /* check to make certain that both sides are available
  463.            this avoids a nasty yet stupid bug in rlogins */
  464. #ifdef PTY_TTY_NAME_SPRINTF
  465.         PTY_TTY_NAME_SPRINTF
  466. #else
  467.             sprintf (pty_name, "/dev/tty%c%x", c, i);
  468. #endif /* no PTY_TTY_NAME_SPRINTF */
  469. #ifndef UNIPLUS
  470.         if (access (pty_name, 6) != 0)
  471.           {
  472.         close (fd);
  473. #if !defined(IRIS) && !defined(__sgi)
  474.         continue;
  475. #else
  476.         return -1;
  477. #endif /* IRIS */
  478.           }
  479. #endif /* not UNIPLUS */
  480.         setup_pty (fd);
  481.         return fd;
  482.       }
  483.       }
  484.   return -1;
  485. }
  486. #endif /* HAVE_PTYS */
  487.  
  488. Lisp_Object
  489. make_process (name)
  490.      Lisp_Object name;
  491. {
  492.   register Lisp_Object val, tem, name1;
  493.   register struct Lisp_Process *p;
  494.   char suffix[10];
  495.   register int i;
  496.  
  497.   /* size of process structure includes the vector header,
  498.      so deduct for that.  But struct Lisp_Vector includes the first
  499.      element, thus deducts too much, so add it back.  */
  500.   val = Fmake_vector (make_number ((sizeof (struct Lisp_Process)
  501.                     - sizeof (struct Lisp_Vector)
  502.                     + sizeof (Lisp_Object))
  503.                    / sizeof (Lisp_Object)),
  504.               Qnil);
  505.   XSETTYPE (val, Lisp_Process);
  506.  
  507.   p = XPROCESS (val);
  508.   XFASTINT (p->infd) = 0;
  509.   XFASTINT (p->outfd) = 0;
  510.   XFASTINT (p->pid) = 0;
  511.   XFASTINT (p->tick) = 0;
  512.   XFASTINT (p->update_tick) = 0;
  513.   p->raw_status_low = Qnil;
  514.   p->raw_status_high = Qnil;
  515.   p->status = Qrun;
  516.   p->mark = Fmake_marker ();
  517.  
  518.   /* If name is already in use, modify it until it is unused.  */
  519.  
  520.   name1 = name;
  521.   for (i = 1; ; i++)
  522.     {
  523.       tem = Fget_process (name1);
  524.       if (NILP (tem)) break;
  525.       sprintf (suffix, "<%d>", i);
  526.       name1 = concat2 (name, build_string (suffix));
  527.     }
  528.   name = name1;
  529.   p->name = name;
  530.   Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
  531.   return val;
  532. }
  533.  
  534. _VOID_
  535. remove_process (proc)
  536.      register Lisp_Object proc;
  537. {
  538.   register Lisp_Object pair;
  539.  
  540.   pair = Frassq (proc, Vprocess_alist);
  541.   Vprocess_alist = Fdelq (pair, Vprocess_alist);
  542.   Fset_marker (XPROCESS (proc)->mark, Qnil, Qnil);
  543.  
  544.   deactivate_process (proc);
  545. }
  546.  
  547. DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0,
  548.   "Return t if OBJECT is a process.")
  549.   (obj)
  550.      Lisp_Object obj;
  551. {
  552.   return XTYPE (obj) == Lisp_Process ? Qt : Qnil;
  553. }
  554.  
  555. DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0,
  556.   "Return the process named NAME, or nil if there is none.")
  557.   (name)
  558.      register Lisp_Object name;
  559. {
  560.   if (XTYPE (name) == Lisp_Process)
  561.     return name;
  562.   CHECK_STRING (name, 0);
  563.   return Fcdr (Fassoc (name, Vprocess_alist));
  564. }
  565.  
  566. DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
  567.   "Return the (or, a) process associated with BUFFER.\n\
  568. BUFFER may be a buffer or the name of one.")
  569.   (name)
  570.      register Lisp_Object name;
  571. {
  572.   register Lisp_Object buf, tail, proc;
  573.  
  574.   if (NILP (name)) return Qnil;
  575.   buf = Fget_buffer (name);
  576.   if (NILP (buf)) return Qnil;
  577.  
  578.   for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
  579.     {
  580.       proc = Fcdr (Fcar (tail));
  581.       if (XTYPE (proc) == Lisp_Process && EQ (XPROCESS (proc)->buffer, buf))
  582.     return proc;
  583.     }
  584.   return Qnil;
  585. }
  586.  
  587. /* This is how commands for the user decode process arguments.  It
  588.    accepts a process, a process name, a buffer, a buffer name, or nil.
  589.    Buffers denote the first process in the buffer, and nil denotes the
  590.    current buffer.  */
  591.  
  592. Lisp_Object
  593. get_process (name)
  594.      register Lisp_Object name;
  595. {
  596.   register Lisp_Object proc;
  597.   if (NILP (name))
  598.     proc = Fget_buffer_process (Fcurrent_buffer ());
  599.   else
  600.     {
  601.       proc = Fget_process (name);
  602.       if (NILP (proc))
  603.     proc = Fget_buffer_process (Fget_buffer (name));
  604.     }
  605.  
  606.   if (!NILP (proc))
  607.     return proc;
  608.  
  609.   if (NILP (name))
  610.     error ("Current buffer has no process");
  611.   else
  612.     error ("Process %s does not exist", XSTRING (name)->data);
  613.   /* NOTREACHED */
  614. }
  615.  
  616. DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0,
  617.   "Delete PROCESS: kill it and forget about it immediately.\n\
  618. PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
  619. nil, indicating the current buffer's process.")
  620.   (proc)
  621.      register Lisp_Object proc;
  622. {
  623.   proc = get_process (proc);
  624.   XPROCESS (proc)->raw_status_low = Qnil;
  625.   XPROCESS (proc)->raw_status_high = Qnil;
  626.   if (NETCONN_P (proc))
  627.     {
  628.       XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (0), Qnil));
  629.       XSETINT (XPROCESS (proc)->tick, ++process_tick);
  630.     }
  631.   else if (XFASTINT (XPROCESS (proc)->infd))
  632.     {
  633.       Fkill_process (proc, Qnil);
  634.       /* Do this now, since remove_process will make sigchld_handler do nothing.  */
  635.       XPROCESS (proc)->status 
  636.     = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil));
  637.       XSETINT (XPROCESS (proc)->tick, ++process_tick);
  638.       status_notify ();
  639.     }
  640.   remove_process (proc);
  641.   return Qnil;
  642. }
  643.  
  644. DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0,
  645.   "Return the status of PROCESS: a symbol, one of these:\n\
  646. run  -- for a process that is running.\n\
  647. stop -- for a process stopped but continuable.\n\
  648. exit -- for a process that has exited.\n\
  649. signal -- for a process that has got a fatal signal.\n\
  650. open -- for a network stream connection that is open.\n\
  651. closed -- for a network stream connection that is closed.\n\
  652. nil -- if arg is a process name and no such process exists.\n\
  653. PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
  654. nil, indicating the current buffer's process.")
  655. /* command -- for a command channel opened to Emacs by another process.\n\
  656.    external -- for an i/o channel opened to Emacs by another process.\n\  */
  657.   (proc)
  658.      register Lisp_Object proc;
  659. {
  660.   register struct Lisp_Process *p;
  661.   register Lisp_Object status;
  662.   proc = get_process (proc);
  663.   if (NILP (proc))
  664.     return proc;
  665.   p = XPROCESS (proc);
  666.   if (!NILP (p->raw_status_low))
  667.     update_status (p);
  668.   status = p->status;
  669.   if (XTYPE (status) == Lisp_Cons)
  670.     status = XCONS (status)->car;
  671.   if (NETCONN_P (proc))
  672.     {
  673.       if (EQ (status, Qrun))
  674.     status = Qopen;
  675.       else if (EQ (status, Qexit))
  676.     status = Qclosed;
  677.     }
  678.   return status;
  679. }
  680.  
  681. DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status,
  682.        1, 1, 0,
  683.   "Return the exit status of PROCESS or the signal number that killed it.\n\
  684. If PROCESS has not yet exited or died, return 0.")
  685.   (proc)
  686.      register Lisp_Object proc;
  687. {
  688.   CHECK_PROCESS (proc, 0);
  689.   if (!NILP (XPROCESS (proc)->raw_status_low))
  690.     update_status (XPROCESS (proc));
  691.   if (XTYPE (XPROCESS (proc)->status) == Lisp_Cons)
  692.     return XCONS (XCONS (XPROCESS (proc)->status)->cdr)->car;
  693.   return make_number (0);
  694. }
  695.  
  696. DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0,
  697.   "Return the process id of PROCESS.\n\
  698. This is the pid of the Unix process which PROCESS uses or talks to.\n\
  699. For a network connection, this value is nil.")
  700.   (proc)
  701.      register Lisp_Object proc;
  702. {
  703.   CHECK_PROCESS (proc, 0);
  704.   return XPROCESS (proc)->pid;
  705. }
  706.  
  707. DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0,
  708.   "Return the name of PROCESS, as a string.\n\
  709. This is the name of the program invoked in PROCESS,\n\
  710. possibly modified to make it unique among process names.")
  711.   (proc)
  712.      register Lisp_Object proc;
  713. {
  714.   CHECK_PROCESS (proc, 0);
  715.   return XPROCESS (proc)->name;
  716. }
  717.  
  718. DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0,
  719.   "Return the command that was executed to start PROCESS.\n\
  720. This is a list of strings, the first string being the program executed\n\
  721. and the rest of the strings being the arguments given to it.\n\
  722. For a non-child channel, this is nil.")
  723.   (proc)
  724.      register Lisp_Object proc;
  725. {
  726.   CHECK_PROCESS (proc, 0);
  727.   return XPROCESS (proc)->command;
  728. }
  729.  
  730. DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer,
  731.   2, 2, 0,
  732.   "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
  733.   (proc, buffer)
  734.      register Lisp_Object proc, buffer;
  735. {
  736.   CHECK_PROCESS (proc, 0);
  737.   if (!NILP (buffer))
  738.     CHECK_BUFFER (buffer, 1);
  739.   XPROCESS (proc)->buffer = buffer;
  740.   return buffer;
  741. }
  742.  
  743. DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer,
  744.   1, 1, 0,
  745.   "Return the buffer PROCESS is associated with.\n\
  746. Output from PROCESS is inserted in this buffer\n\
  747. unless PROCESS has a filter.")
  748.   (proc)
  749.      register Lisp_Object proc;
  750. {
  751.   CHECK_PROCESS (proc, 0);
  752.   return XPROCESS (proc)->buffer;
  753. }
  754.  
  755. DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
  756.   1, 1, 0,
  757.   "Return the marker for the end of the last output from PROCESS.")
  758.   (proc)
  759.      register Lisp_Object proc;
  760. {
  761.   CHECK_PROCESS (proc, 0);
  762.   return XPROCESS (proc)->mark;
  763. }
  764.  
  765. DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
  766.   2, 2, 0,
  767.   "Give PROCESS the filter function FILTER; nil means no filter.\n\
  768. When a process has a filter, each time it does output\n\
  769. the entire string of output is passed to the filter.\n\
  770. The filter gets two arguments: the process and the string of output.\n\
  771. If the process has a filter, its buffer is not used for output.")
  772.   (proc, filter)
  773.      register Lisp_Object proc, filter;
  774. {
  775.   CHECK_PROCESS (proc, 0);
  776.   XPROCESS (proc)->filter = filter;
  777.   return filter;
  778. }
  779.  
  780. DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
  781.   1, 1, 0,
  782.   "Returns the filter function of PROCESS; nil if none.\n\
  783. See `set-process-filter' for more info on filter functions.")
  784.   (proc)
  785.      register Lisp_Object proc;
  786. {
  787.   CHECK_PROCESS (proc, 0);
  788.   return XPROCESS (proc)->filter;
  789. }
  790.  
  791. DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
  792.   2, 2, 0,
  793.   "Give PROCESS the sentinel SENTINEL; nil for none.\n\
  794. The sentinel is called as a function when the process changes state.\n\
  795. It gets two arguments: the process, and a string describing the change.")
  796.   (proc, sentinel)
  797.      register Lisp_Object proc, sentinel;
  798. {
  799.   CHECK_PROCESS (proc, 0);
  800.   XPROCESS (proc)->sentinel = sentinel;
  801.   return sentinel;
  802. }
  803.  
  804. DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
  805.   1, 1, 0,
  806.   "Return the sentinel of PROCESS; nil if none.\n\
  807. See `set-process-sentinel' for more info on sentinels.")
  808.   (proc)
  809.      register Lisp_Object proc;
  810. {
  811.   CHECK_PROCESS (proc, 0);
  812.   return XPROCESS (proc)->sentinel;
  813. }
  814.  
  815. DEFUN ("process-kill-without-query", Fprocess_kill_without_query,
  816.   Sprocess_kill_without_query, 1, 2, 0,
  817.   "Say no query needed if PROCESS is running when Emacs is exited.\n\
  818. Optional second argument if non-nill says to require a query.\n\
  819. Value is t if a query was formerly required.")
  820.   (proc, value)
  821.      register Lisp_Object proc, value;
  822. {
  823.   Lisp_Object tem;
  824.  
  825.   CHECK_PROCESS (proc, 0);
  826.   tem = XPROCESS (proc)->kill_without_query;
  827.   XPROCESS (proc)->kill_without_query = Fnull (value);
  828.  
  829.   return Fnull (tem);
  830. }
  831.  
  832. Lisp_Object
  833. list_processes_1 (dummy)
  834.     /* Needs a parameter */
  835.     Lisp_Object dummy;
  836. {
  837.   register Lisp_Object tail, tem;
  838.   Lisp_Object proc, minspace, tem1;
  839.   register struct buffer *old = current_buffer;
  840.   register struct Lisp_Process *p;
  841.   char tembuf[80];
  842.  
  843.   XFASTINT (minspace) = 1;
  844.  
  845.   set_buffer_internal (XBUFFER (Vstandard_output));
  846.   Fbuffer_disable_undo (Vstandard_output);
  847.  
  848.   current_buffer->truncate_lines = Qt;
  849.  
  850.   write_string ("\
  851. Proc         Status   Buffer         Command\n\
  852. ----         ------   ------         -------\n", -1);
  853.  
  854.   for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
  855.     {
  856.       Lisp_Object symbol;
  857.  
  858.       proc = Fcdr (Fcar (tail));
  859.       p = XPROCESS (proc);
  860.       if (NILP (p->childp))
  861.     continue;
  862.  
  863.       Finsert (1, &p->name);
  864.       Findent_to (make_number (13), minspace);
  865.  
  866.       if (!NILP (p->raw_status_low))
  867.     update_status (p);
  868.       symbol = p->status;
  869.       if (XTYPE (p->status) == Lisp_Cons)
  870.     symbol = XCONS (p->status)->car;
  871.  
  872.       
  873.       if (EQ (symbol, Qsignal))
  874.     {
  875.       Lisp_Object tem;
  876.       tem = Fcar (Fcdr (p->status));
  877. #ifdef VMS
  878.       if (XINT (tem) < NSIG)
  879.         write_string (sys_errlist [XINT (tem)], -1);
  880.       else
  881. #endif
  882.         Fprinc (symbol, Qnil);
  883.     }
  884.       else if (NETCONN_P (proc))
  885.     {
  886.       if (EQ (symbol, Qrun))
  887.         write_string ("open", -1);
  888.       else if (EQ (symbol, Qexit))
  889.         write_string ("closed", -1);
  890.       else
  891.         Fprinc (symbol, Qnil);
  892.     }
  893.       else
  894.     Fprinc (symbol, Qnil);
  895.  
  896.       if (EQ (symbol, Qexit))
  897.     {
  898.       Lisp_Object tem;
  899.       tem = Fcar (Fcdr (p->status));
  900.       if (XFASTINT (tem))
  901.         {
  902.           sprintf (tembuf, " %d", XFASTINT (tem));
  903.           write_string (tembuf, -1);
  904.         }
  905.     }
  906.  
  907.       if (EQ (symbol, Qsignal) || EQ (symbol, Qexit))
  908.     remove_process (proc);
  909.  
  910.       Findent_to (make_number (22), minspace);
  911.       if (NILP (p->buffer))
  912.     insert_string ("(none)");
  913.       else if (NILP (XBUFFER (p->buffer)->name))
  914.     insert_string ("(Killed)");
  915.       else
  916.     Finsert (1, &XBUFFER (p->buffer)->name);
  917.  
  918.       Findent_to (make_number (37), minspace);
  919.  
  920.       if (NETCONN_P (proc))
  921.         {
  922.       sprintf (tembuf, "(network stream connection to %s)\n",
  923.            XSTRING (p->childp)->data);
  924.       insert_string (tembuf);
  925.         }
  926.       else 
  927.     {
  928.       tem = p->command;
  929.       while (1)
  930.         {
  931.           tem1 = Fcar (tem);
  932.           Finsert (1, &tem1);
  933.           tem = Fcdr (tem);
  934.           if (NILP (tem))
  935.         break;
  936.           insert_string (" ");
  937.         }
  938.       insert_string ("\n");
  939.        }
  940.     }
  941.   return Qnil;
  942. }
  943.  
  944. DEFUN ("list-processes", Flist_processes, Slist_processes, 0, 0, "",
  945.   "Display a list of all processes.\n\
  946. \(Any processes listed as Exited or Signaled are actually eliminated\n\
  947. after the listing is made.)")
  948.   ()
  949. {
  950.   internal_with_output_to_temp_buffer ("*Process List*",
  951.                        list_processes_1, Qnil);
  952.   return Qnil;
  953. }
  954.  
  955. DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
  956.   "Return a list of all processes.")
  957.   ()
  958. {
  959.   return Fmapcar (Qcdr, Vprocess_alist);
  960. }
  961.  
  962. /* Starting asynchronous inferior processes.  */
  963.  
  964. DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
  965.   "Start a program in a subprocess.  Return the process object for it.\n\
  966. Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
  967. NAME is name for process.  It is modified if necessary to make it unique.\n\
  968. BUFFER is the buffer or (buffer-name) to associate with the process.\n\
  969.  Process output goes at end of that buffer, unless you specify\n\
  970.  an output stream or filter function to handle the output.\n\
  971.  BUFFER may be also nil, meaning that this process is not associated\n\
  972.  with any buffer\n\
  973. Third arg is program file name.  It is searched for as in the shell.\n\
  974. Remaining arguments are strings to give program as arguments.")
  975.   (nargs, args)
  976.      int nargs;
  977.      register Lisp_Object *args;
  978. {
  979.   Lisp_Object buffer, name, program, proc, current_dir, tem;
  980. #ifdef VMS
  981.   register unsigned char *new_argv;
  982.   int len;
  983. #else
  984.   register unsigned char **new_argv;
  985. #endif
  986.   register int i;
  987.   int count = specpdl_ptr - specpdl;
  988.  
  989.   buffer = args[1];
  990.   if (!NILP (buffer))
  991.     buffer = Fget_buffer_create (buffer);
  992.  
  993.   /* Make sure that the child will be able to chdir to the current
  994.      buffer's current directory, or its unhandled equivalent.  We
  995.      can't just have the child check for an error when it does the
  996.      chdir, since it's in a vfork.
  997.  
  998.      We have to GCPRO around this because Fexpand_file_name and
  999.      Funhandled_file_name_directory might call a file name handling
  1000.      function.  The argument list is protected by the caller, so all
  1001.      we really have to worry about is buffer.  */
  1002.   {
  1003.     struct gcpro gcpro1, gcpro2;
  1004.  
  1005.     current_dir = current_buffer->directory;
  1006.  
  1007.     GCPRO2 (buffer, current_dir);
  1008.  
  1009.     current_dir = 
  1010.       expand_and_dir_to_file
  1011.     (Funhandled_file_name_directory (current_dir), Qnil);
  1012.     if (NILP (Ffile_accessible_directory_p (current_dir)))
  1013.       report_file_error ("Setting current directory",
  1014.              Fcons (current_buffer->directory, Qnil));
  1015.  
  1016.     UNGCPRO;
  1017.   }
  1018.  
  1019.   name = args[0];
  1020.   CHECK_STRING (name, 0);
  1021.  
  1022.   program = args[2];
  1023.  
  1024.   CHECK_STRING (program, 2);
  1025.  
  1026. #ifdef VMS
  1027.   /* Make a one member argv with all args concatenated
  1028.      together separated by a blank.  */
  1029.   len = XSTRING (program)->size + 2;
  1030.   for (i = 3; i < nargs; i++)
  1031.     {
  1032.       tem = args[i];
  1033.       CHECK_STRING (tem, i);
  1034.       len += XSTRING (tem)->size + 1;    /* count the blank */
  1035.     }
  1036.   new_argv = (unsigned char *) alloca (len);
  1037.   strcpy (new_argv, XSTRING (program)->data);
  1038.   for (i = 3; i < nargs; i++)
  1039.     {
  1040.       tem = args[i];
  1041.       CHECK_STRING (tem, i);
  1042.       strcat (new_argv, " ");
  1043.       strcat (new_argv, XSTRING (tem)->data);
  1044.     }
  1045.   /* Need to add code here to check for program existence on VMS */
  1046.   
  1047. #else /* not VMS */
  1048.   new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
  1049.  
  1050.   for (i = 3; i < nargs; i++)
  1051.     {
  1052.       tem = args[i];
  1053.       CHECK_STRING (tem, i);
  1054.       new_argv[i - 2] = XSTRING (tem)->data;
  1055.     }
  1056.   new_argv[i - 2] = 0;
  1057.   new_argv[0] = XSTRING (program)->data;
  1058.  
  1059.   /* If program file name is not absolute, search our path for it */
  1060. #ifdef WINDOWSNT
  1061.   if (new_argv[0][0] != '\\')
  1062. #else
  1063.   if (new_argv[0][0] != '/')
  1064. #endif /* !WINDOWSNT */
  1065.     {
  1066.       tem = Qnil;
  1067.       openp (Vexec_path, program, EXEC_SUFFIXES, &tem, 1);
  1068.       if (NILP (tem))
  1069.     report_file_error ("Searching for program", Fcons (program, Qnil));
  1070.       new_argv[0] = XSTRING (tem)->data;
  1071.     }
  1072. #endif /* not VMS */
  1073.  
  1074.   proc = make_process (name);
  1075.   /* If an error occurs and we can't start the process, we want to
  1076.      remove it from the process list.  This means that each error
  1077.      check in create_process doesn't need to call remove_process
  1078.      itself; it's all taken care of here.  */
  1079.   record_unwind_protect (start_process_unwind, proc);
  1080.  
  1081.   XPROCESS (proc)->childp = Qt;
  1082.   XPROCESS (proc)->command_channel_p = Qnil;
  1083.   XPROCESS (proc)->buffer = buffer;
  1084.   XPROCESS (proc)->sentinel = Qnil;
  1085.   XPROCESS (proc)->filter = Qnil;
  1086.   XPROCESS (proc)->command = Flist (nargs - 2, args + 2);
  1087.  
  1088.   create_process (proc, new_argv, current_dir);
  1089.  
  1090.   return unbind_to (count, proc);
  1091. }
  1092.  
  1093. /* This function is the unwind_protect form for Fstart_process.  If
  1094.    PROC doesn't have its pid set, then we know someone has signalled
  1095.    an error and the process wasn't started successfully, so we should
  1096.    remove it from the process list.  */
  1097. static Lisp_Object
  1098. start_process_unwind (proc)
  1099.      Lisp_Object proc;
  1100. {
  1101.   if (XTYPE (proc) != Lisp_Process)
  1102.     abort ();
  1103.  
  1104.   /* Was PROC started successfully?  */
  1105.   if (XINT (XPROCESS (proc)->pid) <= 0)
  1106.     remove_process (proc);
  1107.  
  1108.   return Qnil;
  1109. }
  1110.  
  1111. SIGTYPE
  1112. create_process_1 (signo)
  1113.      int signo;
  1114. {
  1115. #ifdef USG
  1116.   /* USG systems forget handlers when they are used;
  1117.      must reestablish each time */
  1118.   signal (signo, create_process_1);
  1119. #endif /* USG */
  1120. }
  1121.  
  1122. #if 0  /* This doesn't work; see the note before sigchld_handler.  */
  1123. #ifdef USG
  1124. #ifdef SIGCHLD
  1125. /* Mimic blocking of signals on system V, which doesn't really have it.  */
  1126.  
  1127. /* Nonzero means we got a SIGCHLD when it was supposed to be blocked.  */
  1128. int sigchld_deferred;
  1129.  
  1130. SIGTYPE
  1131. create_process_sigchld ()
  1132. {
  1133.   signal (SIGCHLD, create_process_sigchld);
  1134.  
  1135.   sigchld_deferred = 1;
  1136. }
  1137. #endif
  1138. #endif
  1139. #endif
  1140.  
  1141. #ifndef VMS /* VMS version of this function is in vmsproc.c.  */
  1142. _VOID_
  1143. create_process (process, new_argv, current_dir)
  1144.      Lisp_Object process;
  1145.      char **new_argv;
  1146.      Lisp_Object current_dir;
  1147. {
  1148.   int pid, inchannel, outchannel, forkin, forkout;
  1149.   int sv[2];
  1150. #if 0
  1151. #ifdef SIGCHLD
  1152.   SIGTYPE (*sigchld)();
  1153. #endif
  1154. #endif
  1155.   int pty_flag = 0;
  1156.   extern char **environ;
  1157.  
  1158.   inchannel = outchannel = -1;
  1159.  
  1160. #ifdef HAVE_PTYS
  1161.   if (EQ (Vprocess_connection_type, Qt))
  1162.     outchannel = inchannel = allocate_pty ();
  1163.  
  1164.   if (inchannel >= 0)
  1165.     {
  1166. #ifndef USG 
  1167.       /* On USG systems it does not work to open the pty's tty here
  1168.            and then close and reopen it in the child.  */
  1169. #ifdef O_NOCTTY
  1170.       /* Don't let this terminal become our controlling terminal
  1171.      (in case we don't have one).  */
  1172.       forkout = forkin = open (pty_name, O_RDWR | O_NOCTTY, 0);
  1173. #else
  1174.       forkout = forkin = open (pty_name, O_RDWR, 0);
  1175. #endif
  1176.       if (forkin < 0)
  1177.     report_file_error ("Opening pty", Qnil);
  1178. #else
  1179.       forkin = forkout = -1;
  1180. #endif /* not USG */
  1181.       pty_flag = 1;
  1182.     }
  1183.   else
  1184. #endif /* HAVE_PTYS */
  1185. #ifdef SKTPAIR
  1186.     {
  1187.       if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) < 0)
  1188.     report_file_error ("Opening socketpair", Qnil);
  1189.       outchannel = inchannel = sv[0];
  1190.       forkout = forkin = sv[1];
  1191.     }
  1192. #else /* not SKTPAIR */
  1193.     {
  1194. #ifdef WINDOWSNT
  1195.       /*
  1196.        * A bit of explanation is due here on how we set up the
  1197.        * pipes between the child and the parent.  Since NT doesn't
  1198.        * have fork, we have to set up the pipes using explicitly
  1199.        * inherited handles.  (We are also somewhat constrained by NT's
  1200.        * crt dup routine, which always creates inherited handles.)  
  1201.        * We first create two non-inherited pipes, dup inherited
  1202.        * handles to the read end of one and the write end of the other
  1203.        * (to be used by the child), and then we close the
  1204.        * non-inherited pipe handles we just duped.  This leaves us
  1205.        * two non-inherited handles for use by the parent, and two
  1206.        * complimentary inherited handles for use by the child.
  1207.        *
  1208.        * See callproc.c:child_setup() to see how the child inherits
  1209.        * the handles as stdin, stdout, and stderr.
  1210.        *                                                  (voelker)
  1211.        */
  1212.  
  1213.       _pipe(sv, 0, _O_NOINHERIT);
  1214.       inchannel = sv[0];
  1215.       forkout = dup(sv[1]);
  1216.       close(sv[1]);
  1217.  
  1218.       _pipe(sv, 0, _O_NOINHERIT);
  1219.       outchannel = sv[1];
  1220.       forkin = dup(sv[0]);
  1221.       close(sv[0]);
  1222.  
  1223. #else /* !WINDOWSNT */
  1224.       pipe (sv);
  1225.       inchannel = sv[0];
  1226.       forkout = sv[1];
  1227.       pipe (sv);
  1228.       outchannel = sv[1];
  1229.       forkin = sv[0];
  1230. #endif /* !WINDOWSNT */
  1231.     }
  1232. #endif /* not SKTPAIR */
  1233.  
  1234. #if 0
  1235.   /* Replaced by close_process_descs */
  1236.   set_exclusive_use (inchannel);
  1237.   set_exclusive_use (outchannel);
  1238. #endif
  1239.  
  1240. /* Stride people say it's a mystery why this is needed
  1241.    as well as the O_NDELAY, but that it fails without this.  */
  1242. #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
  1243.   {
  1244.     int one = 1;
  1245.     ioctl (inchannel, FIONBIO, &one);
  1246.   }
  1247. #endif
  1248.  
  1249. #ifdef O_NONBLOCK
  1250.   fcntl (inchannel, F_SETFL, O_NONBLOCK);
  1251. #else
  1252. #ifdef O_NDELAY
  1253.   fcntl (inchannel, F_SETFL, O_NDELAY);
  1254. #endif
  1255. #endif
  1256.  
  1257.   /* Record this as an active process, with its channels.
  1258.      As a result, child_setup will close Emacs's side of the pipes.  */
  1259.   chan_process[inchannel] = process;
  1260.   XFASTINT (XPROCESS (process)->infd) = inchannel;
  1261.   XFASTINT (XPROCESS (process)->outfd) = outchannel;
  1262.   /* Record the tty descriptor used in the subprocess.  */
  1263.   if (forkin < 0)
  1264.     XPROCESS (process)->subtty = Qnil;
  1265.   else
  1266.     XFASTINT (XPROCESS (process)->subtty) = forkin;
  1267.   XPROCESS (process)->pty_flag = (pty_flag ? Qt : Qnil);
  1268.   XPROCESS (process)->status = Qrun;
  1269.  
  1270.   /* Delay interrupts until we have a chance to store
  1271.      the new fork's pid in its process structure */
  1272. #ifdef SIGCHLD
  1273. #ifdef BSD4_1
  1274.   sighold (SIGCHLD);
  1275. #else /* not BSD4_1 */
  1276. #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
  1277.   sigsetmask (sigmask (SIGCHLD));
  1278. #else /* ordinary USG */
  1279. #if 0
  1280.   sigchld_deferred = 0;
  1281.   sigchld = signal (SIGCHLD, create_process_sigchld);
  1282. #endif
  1283. #endif /* ordinary USG */
  1284. #endif /* not BSD4_1 */
  1285. #endif /* SIGCHLD */
  1286.  
  1287.   /* Until we store the proper pid, enable sigchld_handler
  1288.      to recognize an unknown pid as standing for this process.
  1289.      It is very important not to let this `marker' value stay
  1290.      in the table after this function has returned; if it does
  1291.      it might cause call-process to hang and subsequent asynchronous
  1292.      processes to get their return values scrambled.  */
  1293.   XSETINT (XPROCESS (process)->pid, -1);
  1294.  
  1295.   {
  1296.     /* child_setup must clobber environ on systems with true vfork.
  1297.        Protect it from permanent change.  */
  1298.     char **save_environ = environ;
  1299.  
  1300. #ifdef    WINDOWSNT
  1301.     /*
  1302.      * NT doesn't support fork.  Work around it.
  1303.      */
  1304. #else    /* !WINDOWSNT */
  1305.     pid = vfork ();
  1306.     if (pid == 0)
  1307. #endif /* !WINDOWSNT */
  1308.       {
  1309.     int xforkin = forkin;
  1310.     int xforkout = forkout;
  1311.  
  1312. #if 0 /* This was probably a mistake--it duplicates code later on,
  1313.      but fails to handle all the cases.  */
  1314.     /* Make sure SIGCHLD is not blocked in the child.  */
  1315.     sigsetmask (SIGEMPTYMASK);
  1316. #endif
  1317.  
  1318.     /* Make the pty be the controlling terminal of the process.  */
  1319. #ifdef HAVE_PTYS
  1320.     /* First, disconnect its current controlling terminal.  */
  1321. #ifdef HAVE_SETSID
  1322.     setsid ();
  1323. #ifdef TIOCSCTTY
  1324.     /* Make the pty's terminal the controlling terminal.  */
  1325.     if (pty_flag)
  1326.       /* We ignore the return value
  1327.          because faith@cs.unc.edu says that is necessary on Linux.  */
  1328.       ioctl (xforkin, TIOCSCTTY, 0);
  1329. #endif
  1330. #else /* not HAVE_SETSID */
  1331. #if defined (USG) && !defined (IRIX)
  1332.     /* It's very important to call setpgrp() here and no time
  1333.        afterwards.  Otherwise, we lose our controlling tty which
  1334.        is set when we open the pty. */
  1335.     setpgrp ();
  1336. #endif /* USG */
  1337. #endif /* not HAVE_SETSID */
  1338. #ifdef TIOCNOTTY 
  1339.     /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
  1340.        can do TIOCSPGRP only to the process's controlling tty.  */
  1341.     if (pty_flag)
  1342.       {
  1343.         /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here? 
  1344.            I can't test it since I don't have 4.3.  */
  1345.         int j = open ("/dev/tty", O_RDWR, 0);
  1346.         ioctl (j, TIOCNOTTY, 0);
  1347.         close (j);
  1348. #ifndef USG
  1349.         /* In order to get a controlling terminal on some versions
  1350.            of BSD, it is necessary to put the process in pgrp 0
  1351.            before it opens the terminal.  */
  1352.         setpgrp (0, 0);
  1353. #endif
  1354.       }
  1355. #endif /* TIOCNOTTY */
  1356.  
  1357. #if !defined (RTU) && !defined (UNIPLUS)
  1358. /*** There is a suggestion that this ought to be a
  1359.      conditional on TIOCSPGRP.  */
  1360.     /* Now close the pty (if we had it open) and reopen it.
  1361.        This makes the pty the controlling terminal of the subprocess.  */
  1362.     if (pty_flag)
  1363.       {
  1364.         /* I wonder if close (open (pty_name, ...)) would work?  */
  1365.         if (xforkin >= 0)
  1366.           close (xforkin);
  1367.         xforkout = xforkin = open (pty_name, O_RDWR, 0);
  1368.  
  1369.         if (xforkin < 0)
  1370.           abort ();
  1371.       }
  1372. #endif /* not UNIPLUS and not RTU */
  1373. #ifdef SETUP_SLAVE_PTY
  1374.     SETUP_SLAVE_PTY;
  1375. #endif /* SETUP_SLAVE_PTY */
  1376. #ifdef AIX
  1377.     /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
  1378.        Now reenable it in the child, so it will die when we want it to.  */
  1379.     if (pty_flag)
  1380.       signal (SIGHUP, SIG_DFL);
  1381. #endif
  1382. #endif /* HAVE_PTYS */
  1383.  
  1384. #ifdef SIGCHLD
  1385. #ifdef BSD4_1
  1386.     sigrelse (SIGCHLD);
  1387. #else /* not BSD4_1 */
  1388. #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
  1389.     sigsetmask (SIGEMPTYMASK);
  1390. #else /* ordinary USG */
  1391. #if 0
  1392.     signal (SIGCHLD, sigchld);
  1393. #endif
  1394. #endif /* ordinary USG */
  1395. #endif /* not BSD4_1 */
  1396. #endif /* SIGCHLD */
  1397.  
  1398.     child_setup_tty (xforkout);
  1399.     child_setup (xforkin, xforkout, xforkout,
  1400.              new_argv, 1, current_dir
  1401. #ifdef    WINDOWSNT
  1402.              , &pid        // yeech!
  1403. #endif /* WINDOWSNT */
  1404.              );
  1405.     }
  1406.     environ = save_environ;
  1407.   }
  1408.  
  1409.   if (pid < 0)
  1410.     report_file_error ("Doing vfork", Qnil);
  1411.   
  1412.   XFASTINT (XPROCESS (process)->pid) = pid;
  1413.  
  1414.   FD_SET (inchannel, &input_wait_mask);
  1415.   RegisterChild(pid, inchannel);
  1416.  
  1417.   /* If the subfork execv fails, and it exits,
  1418.      this close hangs.  I don't know why.
  1419.      So have an interrupt jar it loose.  */
  1420.   stop_polling ();
  1421. #ifndef WINDOWSNT
  1422.   signal (SIGALRM, create_process_1);
  1423.   alarm (1);
  1424. #endif /* !WINDOWSNT */
  1425. #ifdef SYSV4_PTYS
  1426.   /* OK to close only if it's not a pty.  Otherwise we need to leave
  1427.      it open for ioctl to get pgrp when signals are sent, or to send
  1428.      the interrupt characters through if that's how we're signalling
  1429.      subprocesses.  Alternately if you are concerned about running out
  1430.      of file descriptors, you could just save the tty name and open
  1431.      just to do the ioctl.  */
  1432.   if (NILP (XFASTINT (XPROCESS (process)->pty_flag)))
  1433. #endif
  1434.     {
  1435.       XPROCESS (process)->subtty = Qnil;
  1436.       if (forkin >= 0)
  1437.     close (forkin);
  1438.     }
  1439. #ifndef WINDOWSNT
  1440.   alarm (0);
  1441. #endif /* !WINDOWSNT */
  1442.   start_polling ();
  1443.   if (forkin != forkout && forkout >= 0)
  1444.     close (forkout);
  1445.  
  1446. #ifdef SIGCHLD
  1447. #ifdef BSD4_1
  1448.   sigrelse (SIGCHLD);
  1449. #else /* not BSD4_1 */
  1450. #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
  1451.   sigsetmask (SIGEMPTYMASK);
  1452. #else /* ordinary USG */
  1453. #if 0
  1454.   signal (SIGCHLD, sigchld);
  1455.   /* Now really handle any of these signals
  1456.      that came in during this function.  */
  1457.   if (sigchld_deferred)
  1458. #ifndef WINDOWSNT
  1459.     kill (getpid (), SIGCHLD);
  1460. #endif /* !WINDOWSNT */
  1461. #endif
  1462. #endif /* ordinary USG */
  1463. #endif /* not BSD4_1 */
  1464. #endif /* SIGCHLD */
  1465. }
  1466. #endif /* not VMS */
  1467.  
  1468. #ifdef HAVE_SOCKETS
  1469.  
  1470. /* open a TCP network connection to a given HOST/SERVICE.  Treated
  1471.    exactly like a normal process when reading and writing.  Only
  1472.    differences are in status display and process deletion.  A network
  1473.    connection has no PID; you cannot signal it.  All you can do is
  1474.    deactivate and close it via delete-process */
  1475.  
  1476. DEFUN ("open-network-stream", Fopen_network_stream, Sopen_network_stream, 
  1477.        4, 4, 0, 
  1478.   "Open a TCP connection for a service to a host.\n\
  1479. Returns a subprocess-object to represent the connection.\n\
  1480. Input and output work as for subprocesses; `delete-process' closes it.\n\
  1481. Args are NAME BUFFER HOST SERVICE.\n\
  1482. NAME is name for process.  It is modified if necessary to make it unique.\n\
  1483. BUFFER is the buffer (or buffer-name) to associate with the process.\n\
  1484.  Process output goes at end of that buffer, unless you specify\n\
  1485.  an output stream or filter function to handle the output.\n\
  1486.  BUFFER may be also nil, meaning that this process is not associated\n\
  1487.  with any buffer\n\
  1488. Third arg is name of the host to connect to, or its IP address.\n\
  1489. Fourth arg SERVICE is name of the service desired, or an integer\n\
  1490.  specifying a port number to connect to.")
  1491.    (name, buffer, host, service)
  1492.       Lisp_Object name, buffer, host, service;
  1493. {
  1494.   Lisp_Object proc;
  1495.   register int i;
  1496.   struct sockaddr_in address;
  1497.   struct servent *svc_info;
  1498.   struct hostent *host_info_ptr, host_info;
  1499.   char *(addr_list[2]);
  1500.   unsigned long numeric_addr;
  1501.   int s, outch, inch;
  1502.   char errstring[80];
  1503.   int port;
  1504.   struct hostent host_info_fixed;
  1505.   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  1506.  
  1507.   GCPRO4 (name, buffer, host, service);
  1508.   CHECK_STRING (name, 0);
  1509.   CHECK_STRING (host, 0);
  1510.   if (XTYPE(service) == Lisp_Int)
  1511.     port = htons ((unsigned short) XINT (service));
  1512.   else
  1513.     {
  1514.       CHECK_STRING (service, 0);
  1515.       svc_info = getservbyname (XSTRING (service)->data, "tcp");
  1516.       if (svc_info == 0)
  1517.     error ("Unknown service \"%s\"", XSTRING (service)->data);
  1518.       port = svc_info->s_port;
  1519.     }
  1520.  
  1521.   host_info_ptr = gethostbyname (XSTRING (host)->data);
  1522.   if (host_info_ptr == 0)
  1523.     /* Attempt to interpret host as numeric inet address */
  1524.     {
  1525.       numeric_addr = inet_addr (XSTRING (host)->data);
  1526.       if (numeric_addr == -1)
  1527.     error ("Unknown host \"%s\"", XSTRING (host)->data);
  1528.  
  1529.       host_info_ptr = &host_info;
  1530.       host_info.h_name = 0;
  1531.       host_info.h_aliases = 0;
  1532.       host_info.h_addrtype = AF_INET;
  1533. #ifdef h_addr
  1534.       /* Older machines have only one address slot called h_addr.
  1535.      Newer machines have h_addr_list, but #define h_addr to
  1536.      be its first element.  */
  1537.       host_info.h_addr_list = &(addr_list[0]);
  1538. #endif
  1539.       host_info.h_addr = (char*)(&numeric_addr);
  1540.       addr_list[1] = 0;
  1541.       host_info.h_length = strlen (addr_list[0]);
  1542.     }
  1543.  
  1544.   bzero (&address, sizeof address);
  1545.   bcopy (host_info_ptr->h_addr, (char *) &address.sin_addr,
  1546.      host_info_ptr->h_length);
  1547.   address.sin_family = host_info_ptr->h_addrtype;
  1548.   address.sin_port = port;
  1549.  
  1550.   s = socket (host_info_ptr->h_addrtype, SOCK_STREAM, 0);
  1551.   if (s < 0) 
  1552.     report_file_error ("error creating socket", Fcons (name, Qnil));
  1553.  
  1554.  loop:
  1555.   if (connect (s, (struct sockaddr *) &address, sizeof address) == -1)
  1556.     {
  1557.       int xerrno = errno;
  1558.       if (errno == EINTR)
  1559.     goto loop;
  1560.       close (s);
  1561.       errno = xerrno;
  1562.       report_file_error ("connection failed",
  1563.              Fcons (host, Fcons (name, Qnil)));
  1564.     }
  1565.  
  1566.   inch = s;
  1567.   outch = dup (s);
  1568.   if (outch < 0) 
  1569.     report_file_error ("error duplicating socket", Fcons (name, Qnil));
  1570.  
  1571.   if (!NILP (buffer))
  1572.     buffer = Fget_buffer_create (buffer);
  1573.   proc = make_process (name);
  1574.  
  1575.   chan_process[inch] = proc;
  1576.  
  1577. #ifdef O_NONBLOCK
  1578.   fcntl (inch, F_SETFL, O_NONBLOCK);
  1579. #else
  1580. #ifdef O_NDELAY
  1581.   fcntl (inch, F_SETFL, O_NDELAY);
  1582. #endif
  1583. #endif
  1584.  
  1585.   XPROCESS (proc)->childp = host;
  1586.   XPROCESS (proc)->command_channel_p = Qnil;
  1587.   XPROCESS (proc)->buffer = buffer;
  1588.   XPROCESS (proc)->sentinel = Qnil;
  1589.   XPROCESS (proc)->filter = Qnil;
  1590.   XPROCESS (proc)->command = Qnil;
  1591.   XPROCESS (proc)->pid = Qnil;
  1592.   XFASTINT (XPROCESS (proc)->infd) = s;
  1593.   XFASTINT (XPROCESS (proc)->outfd) = outch;
  1594.   XPROCESS (proc)->status = Qrun;
  1595.   FD_SET (inch, &input_wait_mask);
  1596.  
  1597.   UNGCPRO;
  1598.   return proc;
  1599. }
  1600. #endif    /* HAVE_SOCKETS */
  1601.  
  1602. _VOID_
  1603. deactivate_process (proc)
  1604.      Lisp_Object proc;
  1605. {
  1606.   register int inchannel, outchannel;
  1607.   register struct Lisp_Process *p = XPROCESS (proc);
  1608.  
  1609.   inchannel = XFASTINT (p->infd);
  1610.   outchannel = XFASTINT (p->outfd);
  1611.  
  1612.   if (inchannel)
  1613.     {
  1614.       /* Beware SIGCHLD hereabouts. */
  1615.       flush_pending_output (inchannel);
  1616. #ifdef VMS
  1617.       {
  1618.     VMS_PROC_STUFF *get_vms_process_pointer (), *vs;
  1619.     sys$dassgn (outchannel);
  1620.     vs = get_vms_process_pointer (p->pid);
  1621.     if (vs)
  1622.       give_back_vms_process_stuff (vs);
  1623.       }
  1624. #else
  1625.       close (inchannel);
  1626.       if (outchannel && outchannel != inchannel)
  1627.      close (outchannel);
  1628. #endif
  1629.  
  1630.       XFASTINT (p->infd) = 0;
  1631.       XFASTINT (p->outfd) = 0;
  1632.       chan_process[inchannel] = Qnil;
  1633.       FD_CLR (inchannel, &input_wait_mask);
  1634.     }
  1635. }
  1636.  
  1637. /* Close all descriptors currently in use for communication
  1638.    with subprocess.  This is used in a newly-forked subprocess
  1639.    to get rid of irrelevant descriptors.  */
  1640.  
  1641. _VOID_
  1642. close_process_descs ()
  1643. {
  1644.   int i;
  1645.   for (i = 0; i < MAXDESC; i++)
  1646.     {
  1647.       Lisp_Object process;
  1648.       process = chan_process[i];
  1649.       if (!NILP (process))
  1650.     {
  1651.       int in = XFASTINT (XPROCESS (process)->infd);
  1652.       int out = XFASTINT (XPROCESS (process)->outfd);
  1653.       if (in)
  1654.         close (in);
  1655.       if (out && in != out)
  1656.         close (out);
  1657.     }
  1658.     }
  1659. }
  1660.  
  1661. DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
  1662.   0, 3, 0,
  1663.   "Allow any pending output from subprocesses to be read by Emacs.\n\
  1664. It is read into the process' buffers or given to their filter functions.\n\
  1665. Non-nil arg PROCESS means do not return until some output has been received\n\
  1666. from PROCESS.\n\
  1667. Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\
  1668. seconds and microseconds to wait; return after that much time whether\n\
  1669. or not there is input.\n\
  1670. Return non-nil iff we received any output before the timeout expired.")
  1671.   (proc, timeout, timeout_msecs)
  1672.      register Lisp_Object proc, timeout, timeout_msecs;
  1673. {
  1674.   int seconds;
  1675.   int useconds;
  1676.  
  1677.   if (! NILP (timeout_msecs))
  1678.     {
  1679.       CHECK_NUMBER (timeout_msecs, 2);
  1680.       useconds = XINT (timeout_msecs);
  1681.       if (XTYPE (timeout) != Lisp_Int)
  1682.     XSET (timeout, Lisp_Int, 0);
  1683.  
  1684.       {
  1685.     int carry = useconds / 1000000;
  1686.  
  1687.     XSETINT (timeout, XINT (timeout) + carry);
  1688.     useconds -= carry * 1000000;
  1689.  
  1690.     /* I think this clause is necessary because C doesn't
  1691.        guarantee a particular rounding direction for negative
  1692.        integers.  */
  1693.     if (useconds < 0)
  1694.       {
  1695.         XSETINT (timeout, XINT (timeout) - 1);
  1696.         useconds += 1000000;
  1697.       }
  1698.       }
  1699.     }
  1700.   else
  1701.     useconds = 0;
  1702.  
  1703.   if (! NILP (timeout))
  1704.     {
  1705.       CHECK_NUMBER (timeout, 1);
  1706.       seconds = XINT (timeout);
  1707.       if (seconds <= 0)
  1708.     seconds = -1;
  1709.     }
  1710.   else
  1711.     {
  1712.       if (NILP (proc))
  1713.     seconds = -1;
  1714.       else
  1715.     seconds = 0;
  1716.     }
  1717.  
  1718.   if (NILP (proc))
  1719.     XFASTINT (proc) = 0;
  1720.  
  1721.   return
  1722.     (wait_reading_process_input (seconds, useconds, proc, 0)
  1723.      ? Qt : Qnil);
  1724. }
  1725.  
  1726. /* This variable is different from waiting_for_input in keyboard.c.
  1727.    It is used to communicate to a lisp process-filter/sentinel (via the
  1728.    function Fwaiting_for_user_input_p below) whether emacs was waiting
  1729.    for user-input when that process-filter was called.
  1730.    waiting_for_input cannot be used as that is by definition 0 when
  1731.    lisp code is being evalled */
  1732. static int waiting_for_user_input_p;
  1733.  
  1734. /* Read and dispose of subprocess output while waiting for timeout to
  1735.    elapse and/or keyboard input to be available.
  1736.  
  1737.    TIME_LIMIT is:
  1738.      timeout in seconds, or
  1739.      zero for no limit, or
  1740.      -1 means gobble data immediately available but don't wait for any.
  1741.  
  1742.    MICROSECS is:
  1743.      an additional duration to wait, measured in microseconds.
  1744.      If this is nonzero and time_limit is 0, then the timeout
  1745.      consists of MICROSECS only.
  1746.  
  1747.    READ_KBD is a lisp value:
  1748.      0 to ignore keyboard input, or
  1749.      1 to return when input is available, or
  1750.      -1 meaning caller will actually read the input, so don't throw to
  1751.        the quit handler, or
  1752.      a cons cell, meaning wait wait until its car is non-nil
  1753.        (and gobble terminal input into the buffer if any arrives), or
  1754.      a process object, meaning wait until something arrives from that
  1755.        process.  The return value is true iff we read some input from
  1756.        that process.
  1757.  
  1758.    DO_DISPLAY != 0 means redisplay should be done to show subprocess
  1759.    output that arrives.
  1760.  
  1761.    If READ_KBD is a pointer to a struct Lisp_Process, then the
  1762.      function returns true iff we received input from that process
  1763.      before the timeout elapsed.
  1764.    Otherwise, return true iff we received input from any process.  */
  1765.  
  1766. int
  1767. wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
  1768.      int time_limit, microsecs;
  1769.      Lisp_Object read_kbd;
  1770.      int do_display;
  1771. {
  1772.   register int channel, nfds;
  1773.   static SELECT_TYPE Available;
  1774.   int xerrno;
  1775.   Lisp_Object proc;
  1776.   EMACS_TIME timeout, end_time;
  1777.   SELECT_TYPE Atemp;
  1778.   int wait_channel = 0;
  1779.   struct Lisp_Process *wait_proc = 0;
  1780.   int got_some_input = 0;
  1781.   Lisp_Object *wait_for_cell = 0;
  1782.  
  1783.   FD_ZERO (&Available);
  1784.  
  1785.   /* If read_kbd is a process to watch, set wait_proc and wait_channel
  1786.      accordingly.  */
  1787.   if (XTYPE (read_kbd) == Lisp_Process)
  1788.     {
  1789.       wait_proc = XPROCESS (read_kbd);
  1790.       wait_channel = XFASTINT (wait_proc->infd);
  1791.       XFASTINT (read_kbd) = 0;
  1792.     }
  1793.  
  1794.   /* If waiting for non-nil in a cell, record where.  */
  1795.   if (XTYPE (read_kbd) == Lisp_Cons)
  1796.     {
  1797.       wait_for_cell = &XCONS (read_kbd)->car;
  1798.       XFASTINT (read_kbd) = 0;
  1799.     }
  1800.  
  1801.   waiting_for_user_input_p = XINT (read_kbd);
  1802.  
  1803.   /* Since we may need to wait several times,
  1804.      compute the absolute time to return at.  */
  1805.   if (time_limit || microsecs)
  1806.     {
  1807.       EMACS_GET_TIME (end_time);
  1808.       EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
  1809.       EMACS_ADD_TIME (end_time, end_time, timeout);
  1810.     }
  1811.  
  1812.   while (1)
  1813.     {
  1814.       /* If calling from keyboard input, do not quit
  1815.      since we want to return C-g as an input character.
  1816.      Otherwise, do pending quit if requested.  */
  1817.       if (XINT (read_kbd) >= 0)
  1818.     QUIT;
  1819.  
  1820.       /* Compute time from now till when time limit is up */
  1821.       /* Exit if already run out */
  1822.       if (time_limit == -1)
  1823.     {
  1824.       /* -1 specified for timeout means
  1825.          gobble output available now
  1826.          but don't wait at all. */
  1827.  
  1828.       EMACS_SET_SECS_USECS (timeout, 0, 0);
  1829.     }
  1830.       else if (time_limit || microsecs)
  1831.     {
  1832.       EMACS_GET_TIME (timeout);
  1833.       EMACS_SUB_TIME (timeout, end_time, timeout);
  1834.       if (EMACS_TIME_NEG_P (timeout))
  1835.         break;
  1836.     }
  1837.       else
  1838.     {
  1839.       EMACS_SET_SECS_USECS (timeout, 100000, 0);
  1840.     }
  1841.  
  1842.       /* Cause C-g and alarm signals to take immediate action,
  1843.      and cause input available signals to zero out timeout.
  1844.  
  1845.      It is important that we do this before checking for process
  1846.      activity.  If we get a SIGCHLD after the explicit checks for
  1847.      process activity, timeout is the only way we will know.  */
  1848.       if (XINT (read_kbd) < 0)
  1849.     set_waiting_for_input (&timeout);
  1850.  
  1851.       /* If status of something has changed, and no input is
  1852.      available, notify the user of the change right away.  After
  1853.      this explicit check, we'll let the SIGCHLD handler zap
  1854.      timeout to get our attention.  */
  1855.       if (update_tick != process_tick && do_display)
  1856.     {
  1857.       Atemp = input_wait_mask;
  1858.       EMACS_SET_SECS_USECS (timeout, 0, 0);
  1859.       if (select (MAXDESC, &Atemp, 0, 0, &timeout) <= 0)
  1860.         {
  1861.           /* It's okay for us to do this and then continue with
  1862.          the loop, since timeout has already been zeroed out. */
  1863.           clear_waiting_for_input ();
  1864.           status_notify ();
  1865.         }
  1866.     }
  1867.  
  1868.       /* Don't wait for output from a non-running process.  */
  1869.       if (wait_proc != 0 && !NILP (wait_proc->raw_status_low))
  1870.     update_status (wait_proc);
  1871.       if (wait_proc != 0
  1872.       && ! EQ (wait_proc->status, Qrun))
  1873.     {
  1874.       clear_waiting_for_input ();
  1875.       break;
  1876.     }
  1877.  
  1878.       /* Wait till there is something to do */
  1879.  
  1880.       Available = input_wait_mask;
  1881.       if (! XINT (read_kbd) && wait_for_cell == 0)
  1882.     FD_CLR (0, &Available);
  1883.  
  1884.       /* If frame size has changed or the window is newly mapped,
  1885.      redisplay now, before we start to wait.  There is a race
  1886.      condition here; if a SIGIO arrives between now and the select
  1887.      and indicates that a frame is trashed, the select may block
  1888.      displaying a trashed screen.  */
  1889.       if (frame_garbaged)
  1890.     redisplay_preserve_echo_area ();
  1891.  
  1892.       if (XINT (read_kbd) && detect_input_pending ())
  1893.     nfds = 0;
  1894.       else
  1895.     nfds = select (MAXDESC, &Available, 0, 0, &timeout);
  1896.  
  1897.       xerrno = errno;
  1898.  
  1899.       /* Make C-g and alarm signals set flags again */
  1900.       clear_waiting_for_input ();
  1901.  
  1902.       /*  If we woke up due to SIGWINCH, actually change size now.  */
  1903.       do_pending_window_change ();
  1904.  
  1905.       if (time_limit && nfds == 0) /* timeout elapsed */
  1906.     break;
  1907.       if (nfds < 0)
  1908.     {
  1909.       if (xerrno == EINTR)
  1910.         FD_ZERO (&Available);
  1911. #ifdef ultrix
  1912.       /* Ultrix select seems to return ENOMEM when it is
  1913.          interrupted.  Treat it just like EINTR.  Bleah.  Note
  1914.          that we want to test for the "ultrix" CPP symbol, not
  1915.          "__ultrix__"; the latter is only defined under GCC, but
  1916.          not by DEC's bundled CC.  -JimB  */
  1917.       else if (xerrno == ENOMEM)
  1918.         FD_ZERO (&Available);
  1919. #endif
  1920. #ifdef ALLIANT
  1921.       /* This happens for no known reason on ALLIANT.
  1922.          I am guessing that this is the right response. -- RMS.  */
  1923.       else if (xerrno == EFAULT)
  1924.         FD_ZERO (&Available);
  1925. #endif
  1926.       else if (xerrno == EBADF)
  1927.         {
  1928. #ifdef AIX
  1929.           /* AIX doesn't handle PTY closure the same way BSD does.  On AIX,
  1930.          the child's closure of the pts gives the parent a SIGHUP, and
  1931.          the ptc file descriptor is automatically closed,
  1932.          yielding EBADF here or at select() call above.
  1933.          So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
  1934.          in m-ibmrt-aix.h), and here we just ignore the select error.
  1935.          Cleanup occurs c/o status_notify after SIGCLD. */
  1936.           FD_ZERO (&Available); /* Cannot depend on values returned */
  1937. #else
  1938.           abort ();
  1939. #endif
  1940.         }
  1941.       else
  1942.         error("select error: %s", sys_errlist[xerrno]);
  1943.     }
  1944. #if defined(sun) && !defined(USG5_4)
  1945.       else if (nfds > 0 && FD_ISSET (0, &Available) && interrupt_input)
  1946.     /* System sometimes fails to deliver SIGIO.
  1947.  
  1948.        David J. Mackenzie says that Emacs doesn't compile under
  1949.        Solaris if this code is enabled, thus the USG5_4 in the CPP
  1950.        conditional.  "I haven't noticed any ill effects so far.
  1951.        If you find a Solaris expert somewhere, they might know
  1952.        better." */
  1953.     kill (getpid (), SIGIO);
  1954. #endif
  1955.  
  1956.       /* Check for keyboard input */
  1957.       /* If there is any, return immediately
  1958.      to give it higher priority than subprocesses */
  1959.  
  1960.       if ((XINT (read_kbd) || wait_for_cell)
  1961.       && detect_input_pending ())
  1962.     {
  1963.       swallow_events ();
  1964.       if (detect_input_pending ())
  1965.         break;
  1966.     }
  1967.  
  1968.       /* Exit now if the cell we're waiting for became non-nil.  */
  1969.       if (wait_for_cell && ! NILP (*wait_for_cell))
  1970.     break;
  1971.  
  1972. #ifdef SIGIO
  1973.       /* If we think we have keyboard input waiting, but didn't get SIGIO
  1974.      go read it.  This can happen with X on BSD after logging out.
  1975.      In that case, there really is no input and no SIGIO,
  1976.      but select says there is input.  */
  1977.  
  1978.       if (XINT (read_kbd) && interrupt_input && (FD_ISSET (fileno (stdin), &Available)))
  1979.     kill (0, SIGIO);
  1980. #endif
  1981.  
  1982.       if (! wait_proc)
  1983.     got_some_input |= nfds > 0;
  1984.  
  1985.       /* If checking input just got us a size-change event from X,
  1986.      obey it now if we should.  */
  1987.       if (XINT (read_kbd) || wait_for_cell)
  1988.     do_pending_window_change ();
  1989.  
  1990.       /* Check for data from a process or a command channel */
  1991.       for (channel = FIRST_PROC_DESC; channel < MAXDESC; channel++)
  1992.     {
  1993.       if (FD_ISSET (channel, &Available))
  1994.         {
  1995.           int nread;
  1996.  
  1997.           /* If waiting for this channel, arrange to return as
  1998.          soon as no more input to be processed.  No more
  1999.          waiting.  */
  2000.           if (wait_channel == channel)
  2001.         {
  2002.           wait_channel = 0;
  2003.           time_limit = -1;
  2004.           got_some_input = 1;
  2005.         }
  2006.           proc = chan_process[channel];
  2007.           if (NILP (proc))
  2008.         continue;
  2009.  
  2010.           /* Read data from the process, starting with our
  2011.          buffered-ahead character if we have one.  */
  2012.  
  2013.           nread = read_process_output (proc, channel);
  2014.           if (nread > 0)
  2015.         {
  2016.           /* Since read_process_output can run a filter,
  2017.              which can call accept-process-output,
  2018.              don't try to read from any other processes
  2019.              before doing the select again.  */
  2020.           FD_ZERO (&Available);
  2021.  
  2022.           if (do_display)
  2023.             redisplay_preserve_echo_area ();
  2024.         }
  2025. #ifdef EWOULDBLOCK
  2026.           else if (nread == -1 && errno == EWOULDBLOCK)
  2027.         ;
  2028. #else
  2029. #ifdef O_NONBLOCK
  2030.           else if (nread == -1 && errno == EAGAIN)
  2031.         ;
  2032. #else
  2033. #ifdef O_NDELAY
  2034.           else if (nread == -1 && errno == EAGAIN)
  2035.         ;
  2036.           /* Note that we cannot distinguish between no input
  2037.          available now and a closed pipe.
  2038.          With luck, a closed pipe will be accompanied by
  2039.          subprocess termination and SIGCHLD.  */
  2040.           else if (nread == 0 && !NETCONN_P (proc))
  2041.         ;
  2042. #endif                /* O_NDELAY */
  2043. #endif                /* O_NONBLOCK */
  2044. #endif                /* EWOULDBLOCK */
  2045. #ifdef HAVE_PTYS
  2046.           /* On some OSs with ptys, when the process on one end of
  2047.          a pty exits, the other end gets an error reading with
  2048.          errno = EIO instead of getting an EOF (0 bytes read).
  2049.          Therefore, if we get an error reading and errno =
  2050.          EIO, just continue, because the child process has
  2051.          exited and should clean itself up soon (e.g. when we
  2052.          get a SIGCHLD). */
  2053.           else if (nread == -1 && errno == EIO)
  2054.         ;
  2055. #endif                /* HAVE_PTYS */
  2056.           /* If we can detect process termination, don't consider the process
  2057.          gone just because its pipe is closed.  */
  2058. #ifdef SIGCHLD
  2059.           else if (nread == 0 && !NETCONN_P (proc))
  2060.         ;
  2061. #endif
  2062.           else
  2063.         {
  2064.           /* Preserve status of processes already terminated.  */
  2065.           XSETINT (XPROCESS (proc)->tick, ++process_tick);
  2066.           deactivate_process (proc);
  2067.           if (!NILP (XPROCESS (proc)->raw_status_low))
  2068.             update_status (XPROCESS (proc));
  2069.           if (EQ (XPROCESS (proc)->status, Qrun))
  2070.             XPROCESS (proc)->status
  2071.               = Fcons (Qexit, Fcons (make_number (256), Qnil));
  2072.         }
  2073.         }
  2074.     }            /* end for each file descriptor */
  2075.     }                /* end while exit conditions not met */
  2076.  
  2077.   /* If calling from keyboard input, do not quit
  2078.      since we want to return C-g as an input character.
  2079.      Otherwise, do pending quit if requested.  */
  2080.   if (XINT (read_kbd) >= 0)
  2081.     {
  2082.       /* Prevent input_pending from remaining set if we quit.  */
  2083.       clear_input_pending ();
  2084.       QUIT;
  2085.     }
  2086.  
  2087.   return got_some_input;
  2088. }
  2089.  
  2090. /* Read pending output from the process channel,
  2091.    starting with our buffered-ahead character if we have one.
  2092.    Yield number of characters read.
  2093.  
  2094.    This function reads at most 1024 characters.
  2095.    If you want to read all available subprocess output,
  2096.    you must call it repeatedly until it returns zero.  */
  2097.  
  2098. int
  2099. read_process_output (proc, channel)
  2100.      Lisp_Object proc;
  2101.      register int channel;
  2102. {
  2103.   register int nchars;
  2104. #ifdef VMS
  2105.   char *chars;
  2106. #else
  2107.   char chars[1024];
  2108. #endif
  2109.   register Lisp_Object outstream;
  2110.   register struct buffer *old = current_buffer;
  2111.   register struct Lisp_Process *p = XPROCESS (proc);
  2112.   register int opoint;
  2113.  
  2114. #ifdef VMS
  2115.   VMS_PROC_STUFF *vs, *get_vms_process_pointer();
  2116.  
  2117.   vs = get_vms_process_pointer (p->pid);
  2118.   if (vs)
  2119.     {
  2120.       if (!vs->iosb[0])
  2121.     return(0);        /* Really weird if it does this */
  2122.       if (!(vs->iosb[0] & 1))
  2123.     return -1;        /* I/O error */
  2124.     }
  2125.   else
  2126.     error ("Could not get VMS process pointer");
  2127.   chars = vs->inputBuffer;
  2128.   nchars = clean_vms_buffer (chars, vs->iosb[1]);
  2129.   if (nchars <= 0)
  2130.     {
  2131.       start_vms_process_read (vs); /* Crank up the next read on the process */
  2132.       return 1;            /* Nothing worth printing, say we got 1 */
  2133.     }
  2134. #else /* not VMS */
  2135.  
  2136.   if (proc_buffered_char[channel] < 0)
  2137.     nchars = read (channel, chars, sizeof chars);
  2138.   else
  2139.     {
  2140.         chars[0] = proc_buffered_char[channel];
  2141.         proc_buffered_char[channel] = -1;
  2142. #ifdef WINDOWSNT
  2143.         nchars = ReadChildOutput(channel, chars+1, sizeof(chars)-1);
  2144. #else
  2145.         nchars = read (channel, chars + 1, sizeof chars - 1);
  2146. #endif
  2147.         if (nchars < 0)
  2148.             nchars = 1;
  2149.         else
  2150.             nchars = nchars + 1;
  2151.     }
  2152. #endif /* not VMS */
  2153.  
  2154.   if (nchars <= 0) return nchars;
  2155.  
  2156.   outstream = p->filter;
  2157.   if (!NILP (outstream))
  2158.     {
  2159.       /* We inhibit quit here instead of just catching it so that 
  2160.      hitting ^G when a filter happens to be running won't screw
  2161.      it up.  */
  2162.       int count = specpdl_ptr - specpdl;
  2163.       Lisp_Object odeactivate;
  2164.  
  2165.       odeactivate = Vdeactivate_mark;
  2166.  
  2167.       specbind (Qinhibit_quit, Qt);
  2168.       call2 (outstream, proc, make_string (chars, nchars));
  2169.  
  2170.       /* Handling the process output should not deactivate the mark.  */
  2171.       Vdeactivate_mark = odeactivate;
  2172.  
  2173. #ifdef VMS
  2174.       start_vms_process_read (vs);
  2175. #endif
  2176.       unbind_to (count, Qnil);
  2177.       return nchars;
  2178.     }
  2179.  
  2180.   /* If no filter, write into buffer if it isn't dead.  */
  2181.   if (!NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
  2182.     {
  2183.       Lisp_Object old_read_only;
  2184.       Lisp_Object old_begv, old_zv;
  2185.       Lisp_Object odeactivate;
  2186.  
  2187.       odeactivate = Vdeactivate_mark;
  2188.  
  2189.       Fset_buffer (p->buffer);
  2190.       opoint = point;
  2191.       old_read_only = current_buffer->read_only;
  2192.       XFASTINT (old_begv) = BEGV;
  2193.       XFASTINT (old_zv) = ZV;
  2194.  
  2195.       current_buffer->read_only = Qnil;
  2196.  
  2197.       /* Insert new output into buffer
  2198.      at the current end-of-output marker,
  2199.      thus preserving logical ordering of input and output.  */
  2200.       if (XMARKER (p->mark)->buffer)
  2201.     SET_PT (marker_position (p->mark));
  2202.       else
  2203.     SET_PT (ZV);
  2204.  
  2205.       /* If the output marker is outside of the visible region, save
  2206.      the restriction and widen.  */
  2207.       if (! (BEGV <= point && point <= ZV))
  2208.     Fwiden ();
  2209.  
  2210.       /* Make sure opoint floats ahead of any new text, just as point
  2211.      would.  */
  2212.       if (point <= opoint)
  2213.     opoint += nchars;
  2214.  
  2215.       /* Insert after old_begv, but before old_zv.  */
  2216.       if (point < XFASTINT (old_begv))
  2217.     XFASTINT (old_begv) += nchars;
  2218.       if (point <= XFASTINT (old_zv))
  2219.     XFASTINT (old_zv) += nchars;
  2220.  
  2221.       /* Insert before markers in case we are inserting where
  2222.      the buffer's mark is, and the user's next command is Meta-y.  */
  2223.       insert_before_markers (chars, nchars);
  2224.       Fset_marker (p->mark, make_number (point), p->buffer);
  2225.  
  2226.       update_mode_lines++;
  2227.  
  2228.       /* If the restriction isn't what it should be, set it.  */
  2229.       if (XFASTINT (old_begv) != BEGV || XFASTINT (old_zv) != ZV)
  2230.     Fnarrow_to_region (old_begv, old_zv);
  2231.  
  2232.       /* Handling the process output should not deactivate the mark.  */
  2233.       Vdeactivate_mark = odeactivate;
  2234.  
  2235.       current_buffer->read_only = old_read_only;
  2236.       SET_PT (opoint);
  2237.       set_buffer_internal (old);
  2238.     }
  2239. #ifdef VMS
  2240.   start_vms_process_read (vs);
  2241. #endif
  2242.   return nchars;
  2243. }
  2244.  
  2245. DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, Swaiting_for_user_input_p,
  2246.        0, 0, 0,
  2247.   "Returns non-NIL if emacs is waiting for input from the user.\n\
  2248. This is intended for use by asynchronous process output filters and sentinels.")
  2249.        ()
  2250. {
  2251.   return ((waiting_for_user_input_p) ? Qt : Qnil);
  2252. }
  2253.  
  2254. /* Sending data to subprocess */
  2255.  
  2256. jmp_buf send_process_frame;
  2257.  
  2258. SIGTYPE
  2259. send_process_trap ()
  2260. {
  2261. #ifdef BSD4_1
  2262.   sigrelse (SIGPIPE);
  2263.   sigrelse (SIGALRM);
  2264. #endif /* BSD4_1 */
  2265.   longjmp (send_process_frame, 1);
  2266. }
  2267.  
  2268. _VOID_
  2269. send_process (proc, buf, len)
  2270.      Lisp_Object proc;
  2271.      char *buf;
  2272.      int len;
  2273. {
  2274.   /* Don't use register vars; longjmp can lose them.  */
  2275.   int rv;
  2276.   unsigned char *procname = XSTRING (XPROCESS (proc)->name)->data;
  2277.  
  2278.  
  2279. #ifdef VMS
  2280.   struct Lisp_Process *p = XPROCESS (proc);
  2281.   VMS_PROC_STUFF *vs, *get_vms_process_pointer();
  2282. #endif /* VMS */
  2283.  
  2284.   if (! NILP (XPROCESS (proc)->raw_status_low))
  2285.     update_status (XPROCESS (proc));
  2286.   if (! EQ (XPROCESS (proc)->status, Qrun))
  2287.     error ("Process %s not running", procname);
  2288.  
  2289. #ifdef VMS
  2290.   vs = get_vms_process_pointer (p->pid);
  2291.   if (vs == 0)
  2292.     error ("Could not find this process: %x", p->pid);
  2293.   else if (write_to_vms_process (vs, buf, len))
  2294.     ;
  2295. #else
  2296.   if (!setjmp (send_process_frame))
  2297.     while (len > 0)
  2298.       {
  2299.     int this = len;
  2300. #ifndef WINDOWSNT
  2301.     SIGTYPE (*old_sigpipe)();
  2302. #endif
  2303.  
  2304.     /* Don't send more than 500 bytes at a time.  */
  2305.     if (this > 500)
  2306.       this = 500;
  2307. #ifndef WINDOWSNT
  2308.     old_sigpipe = (SIGTYPE (*) ()) signal (SIGPIPE, send_process_trap);
  2309. #endif /* !WINDOWSNT */
  2310.     rv = write (XFASTINT (XPROCESS (proc)->outfd), buf, this);
  2311. #ifndef WINDOWSNT
  2312.     signal (SIGPIPE, old_sigpipe);
  2313. #endif /* !WINDOWSNT */
  2314.     if (rv < 0)
  2315.       {
  2316.         if (0
  2317. #ifdef EWOULDBLOCK
  2318.         || errno == EWOULDBLOCK
  2319. #endif
  2320. #ifdef EAGAIN
  2321.         || errno == EAGAIN
  2322. #endif
  2323.         )
  2324.           {
  2325.         /* It would be nice to accept process output here,
  2326.            but that is difficult.  For example, it could
  2327.            garbage what we are sending if that is from a buffer.  */
  2328.         immediate_quit = 1;
  2329.         QUIT;
  2330.         sleep (1);
  2331.         immediate_quit = 0;
  2332.         continue;
  2333.           }
  2334.         report_file_error ("writing to process", Fcons (proc, Qnil));
  2335.       }
  2336.     buf += rv;
  2337.     len -= rv;
  2338.     /* Allow input from processes between bursts of sending.
  2339.        Otherwise things may get stopped up.  */
  2340.     if (len > 0)
  2341.       {
  2342.         Lisp_Object zero;
  2343.  
  2344.         XFASTINT (zero) = 0;
  2345.         wait_reading_process_input (-1, 0, zero, 0);
  2346.       }
  2347.       }
  2348. #endif
  2349.   else
  2350.     {
  2351.       XPROCESS (proc)->raw_status_low = Qnil;
  2352.       XPROCESS (proc)->raw_status_high = Qnil;
  2353.       XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
  2354.       XSETINT (XPROCESS (proc)->tick, ++process_tick);
  2355.       deactivate_process (proc);
  2356. #ifdef VMS
  2357.       error ("Error writing to process %s; closed it", procname);
  2358. #else
  2359.       error ("SIGPIPE raised on process %s; closed it", procname);
  2360. #endif
  2361.     }
  2362. }
  2363.  
  2364. DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
  2365.   3, 3, 0,
  2366.   "Send current contents of region as input to PROCESS.\n\
  2367. PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
  2368. nil, indicating the current buffer's process.\n\
  2369. Called from program, takes three arguments, PROCESS, START and END.\n\
  2370. If the region is more than 500 characters long,\n\
  2371. it is sent in several bunches.  This may happen even for shorter regions.\n\
  2372. Output from processes can arrive in between bunches.")
  2373.   (process, start, end)
  2374.      Lisp_Object process, start, end;
  2375. {
  2376.   Lisp_Object proc;
  2377.   int start1;
  2378.  
  2379.   proc = get_process (process);
  2380.   validate_region (&start, &end);
  2381.  
  2382.   if (XINT (start) < GPT && XINT (end) > GPT)
  2383.     move_gap (start);
  2384.  
  2385.   start1 = XINT (start);
  2386.   send_process (proc, &FETCH_CHAR (start1), XINT (end) - XINT (start));
  2387.  
  2388.   return Qnil;
  2389. }
  2390.  
  2391. DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string,
  2392.   2, 2, 0,
  2393.   "Send PROCESS the contents of STRING as input.\n\
  2394. PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
  2395. nil, indicating the current buffer's process.\n\
  2396. If STRING is more than 500 characters long,\n\
  2397. it is sent in several bunches.  This may happen even for shorter strings.\n\
  2398. Output from processes can arrive in between bunches.")
  2399.   (process, string)
  2400.      Lisp_Object process, string;
  2401. {
  2402.   Lisp_Object proc;
  2403.   CHECK_STRING (string, 1);
  2404.   proc = get_process (process);
  2405.   send_process (proc, XSTRING (string)->data, XSTRING (string)->size);
  2406.   return Qnil;
  2407. }
  2408.  
  2409. /* send a signal number SIGNO to PROCESS.
  2410.    CURRENT_GROUP means send to the process group that currently owns
  2411.    the terminal being used to communicate with PROCESS.
  2412.    This is used for various commands in shell mode.
  2413.    If NOMSG is zero, insert signal-announcements into process's buffers
  2414.    right away.
  2415.  
  2416.    If we can, we try to signal PROCESS by sending control characters
  2417.    down the pipe.  This allows us to signal inferiors who have changed
  2418.    their uid, for which killpg would return an EPERM error.  */
  2419.  
  2420. static void
  2421. process_send_signal (process, signo, current_group, nomsg)
  2422.      Lisp_Object process;
  2423.      int signo;
  2424.      Lisp_Object current_group;
  2425.      int nomsg;
  2426. {
  2427.   Lisp_Object proc;
  2428.   register struct Lisp_Process *p;
  2429.   int gid;
  2430.   int no_pgrp = 0;
  2431.  
  2432.   proc = get_process (process);
  2433.   p = XPROCESS (proc);
  2434.  
  2435.   if (!EQ (p->childp, Qt))
  2436.     error ("Process %s is not a subprocess",
  2437.        XSTRING (p->name)->data);
  2438.   if (!XFASTINT (p->infd))
  2439.     error ("Process %s is not active",
  2440.        XSTRING (p->name)->data);
  2441.  
  2442.   if (NILP (p->pty_flag))
  2443.     current_group = Qnil;
  2444.  
  2445.   /* If we are using pgrps, get a pgrp number and make it negative.  */
  2446.   if (!NILP (current_group))
  2447.     {
  2448. #ifdef SIGNALS_VIA_CHARACTERS
  2449.       /* If possible, send signals to the entire pgrp
  2450.      by sending an input character to it.  */
  2451.  
  2452.       /* TERMIOS is the latest and bestest, and seems most likely to
  2453.          work.  If the system has it, use it.  */
  2454. #ifdef HAVE_TERMIOS
  2455.       struct termios t;
  2456.  
  2457.       switch (signo)
  2458.     {
  2459.     case SIGINT:
  2460.       tcgetattr (XFASTINT (p->infd), &t);
  2461.       send_process (proc, &t.c_cc[VINTR], 1);
  2462.       return;
  2463.  
  2464.     case SIGQUIT:
  2465.       tcgetattr (XFASTINT (p->infd), &t);
  2466.         send_process (proc, &t.c_cc[VQUIT], 1);
  2467.         return;
  2468.  
  2469.       case SIGTSTP:
  2470.       tcgetattr (XFASTINT (p->infd), &t);
  2471. #ifdef VSWTCH
  2472.         send_process (proc, &t.c_cc[VSWTCH], 1);
  2473. #else
  2474.       send_process (proc, &t.c_cc[VSUSP], 1);
  2475. #endif
  2476.         return;
  2477.     }
  2478.  
  2479. #else /* ! HAVE_TERMIOS */
  2480.  
  2481.       /* On Berkeley descendants, the following IOCTL's retrieve the
  2482.      current control characters.  */
  2483. #if defined (TIOCGLTC) && defined (TIOCGETC)
  2484.  
  2485.       struct tchars c;
  2486.       struct ltchars lc;
  2487.  
  2488.       switch (signo)
  2489.     {
  2490.     case SIGINT:
  2491.       ioctl (XFASTINT (p->infd), TIOCGETC, &c);
  2492.       send_process (proc, &c.t_intrc, 1);
  2493.       return;
  2494.     case SIGQUIT:
  2495.       ioctl (XFASTINT (p->infd), TIOCGETC, &c);
  2496.       send_process (proc, &c.t_quitc, 1);
  2497.       return;
  2498. #ifdef SIGTSTP
  2499.     case SIGTSTP:
  2500.       ioctl (XFASTINT (p->infd), TIOCGLTC, &lc);
  2501.       send_process (proc, &lc.t_suspc, 1);
  2502.       return;
  2503. #endif /* ! defined (SIGTSTP) */
  2504.     }
  2505.  
  2506. #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
  2507.  
  2508.       /* On SYSV descendants, the TCGETA ioctl retrieves the current control
  2509.      characters.  */
  2510. #ifdef TCGETA
  2511.       struct termio t;
  2512.       switch (signo)
  2513.     {
  2514.     case SIGINT:
  2515.       ioctl (XFASTINT (p->infd), TCGETA, &t);
  2516.       send_process (proc, &t.c_cc[VINTR], 1);
  2517.       return;
  2518.     case SIGQUIT:
  2519.       ioctl (XFASTINT (p->infd), TCGETA, &t);
  2520.       send_process (proc, &t.c_cc[VQUIT], 1);
  2521.       return;
  2522. #ifdef SIGTSTP
  2523.     case SIGTSTP:
  2524.       ioctl (XFASTINT (p->infd), TCGETA, &t);
  2525.       send_process (proc, &t.c_cc[VSWTCH], 1);
  2526.       return;
  2527. #endif /* ! defined (SIGTSTP) */
  2528.     }
  2529. #else /* ! defined (TCGETA) */
  2530.       Your configuration files are messed up.
  2531.       /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
  2532.      you'd better be using one of the alternatives above!  */
  2533. #endif /* ! defined (TCGETA) */
  2534. #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
  2535. #endif /* ! defined HAVE_TERMIOS */
  2536. #endif /* ! defined (SIGNALS_VIA_CHARACTERS) */
  2537.  
  2538. #ifdef TIOCGPGRP 
  2539.       /* Get the pgrp using the tty itself, if we have that.
  2540.      Otherwise, use the pty to get the pgrp.
  2541.      On pfa systems, saka@pfu.fujitsu.co.JP writes:
  2542.      "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
  2543.      But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
  2544.      His patch indicates that if TIOCGPGRP returns an error, then
  2545.      we should just assume that p->pid is also the process group id.  */
  2546.       {
  2547.     int err;
  2548.  
  2549.     if (!NILP (p->subtty))
  2550.       err = ioctl (XFASTINT (p->subtty), TIOCGPGRP, &gid);
  2551.     else
  2552.       err = ioctl (XFASTINT (p->infd), TIOCGPGRP, &gid);
  2553.  
  2554. #ifdef pfa
  2555.     if (err == -1)
  2556.       gid = - XFASTINT (p->pid);
  2557. #endif /* ! defined (pfa) */
  2558.       }
  2559.       if (gid == -1)
  2560.     no_pgrp = 1;
  2561.       else
  2562.     gid = - gid;
  2563. #else  /* ! defined (TIOCGPGRP ) */
  2564.       /* Can't select pgrps on this system, so we know that
  2565.      the child itself heads the pgrp.  */
  2566.       gid = - XFASTINT (p->pid);
  2567. #endif /* ! defined (TIOCGPGRP ) */
  2568.     }
  2569.   else
  2570.     gid = - XFASTINT (p->pid);
  2571.  
  2572.   switch (signo)
  2573.     {
  2574. #ifdef SIGCONT
  2575.     case SIGCONT:
  2576.       p->raw_status_low = Qnil;
  2577.       p->raw_status_high = Qnil;
  2578.       p->status = Qrun;
  2579.       XSETINT (p->tick, ++process_tick);
  2580.       if (!nomsg)
  2581.     status_notify ();
  2582.       break;
  2583. #endif /* ! defined (SIGCONT) */
  2584. #ifndef WINDOWSNT
  2585.     case SIGINT:
  2586. #ifdef VMS
  2587.       send_process (proc, "\003", 1);    /* ^C */
  2588.       goto whoosh;
  2589. #endif
  2590.     case SIGQUIT:
  2591. #ifdef VMS
  2592.       send_process (proc, "\031", 1);    /* ^Y */
  2593.       goto whoosh;
  2594. #endif
  2595.     case SIGKILL:
  2596. #ifdef VMS
  2597.       sys$forcex (&(XFASTINT (p->pid)), 0, 1);
  2598.       whoosh:
  2599. #endif
  2600.       flush_pending_output (XFASTINT (p->infd));
  2601.       break;
  2602. #endif /* !WINDOWSNT */
  2603.     }
  2604.  
  2605.   /* If we don't have process groups, send the signal to the immediate
  2606.      subprocess.  That isn't really right, but it's better than any
  2607.      obvious alternative.  */
  2608.   if (no_pgrp)
  2609.     {
  2610. #ifndef WINDOWSNT
  2611.       kill (XFASTINT (p->pid), signo);
  2612. #endif /* !WINDOWSNT */
  2613.       return;
  2614.     }
  2615.  
  2616.   /* gid may be a pid, or minus a pgrp's number */
  2617. #ifdef TIOCSIGSEND
  2618.   if (!NILP (current_group))
  2619.     ioctl (XFASTINT (p->infd), TIOCSIGSEND, signo);
  2620.   else
  2621.     {
  2622.       gid = - XFASTINT (p->pid);
  2623.       kill (gid, signo);
  2624.     }
  2625. #else /* ! defined (TIOCSIGSEND) */
  2626.   EMACS_KILLPG (-gid, signo);
  2627. #endif /* ! defined (TIOCSIGSEND) */
  2628. }
  2629.  
  2630. DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0,
  2631.   "Interrupt process PROCESS.  May be process or name of one.\n\
  2632. PROCESS may be a process, a buffer, or the name of a process or buffer.\n\
  2633. Nil or no arg means current buffer's process.\n\
  2634. Second arg CURRENT-GROUP non-nil means send signal to\n\
  2635. the current process-group of the process's controlling terminal\n\
  2636. rather than to the process's own process group.\n\
  2637. If the process is a shell, this means interrupt current subjob\n\
  2638. rather than the shell.")
  2639.   (process, current_group)
  2640.      Lisp_Object process, current_group;
  2641. {
  2642.   process_send_signal (process, SIGINT, current_group, 0);
  2643.   return process;
  2644. }
  2645.  
  2646. DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0,
  2647.   "Kill process PROCESS.  May be process or name of one.\n\
  2648. See function `interrupt-process' for more details on usage.")
  2649.   (process, current_group)
  2650.      Lisp_Object process, current_group;
  2651. {
  2652.   process_send_signal (process, SIGKILL, current_group, 0);
  2653.   return process;
  2654. }
  2655.  
  2656. DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0,
  2657.   "Send QUIT signal to process PROCESS.  May be process or name of one.\n\
  2658. See function `interrupt-process' for more details on usage.")
  2659.   (process, current_group)
  2660.      Lisp_Object process, current_group;
  2661. {
  2662.   process_send_signal (process, SIGQUIT, current_group, 0);
  2663.   return process;
  2664. }
  2665.  
  2666. DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0,
  2667.   "Stop process PROCESS.  May be process or name of one.\n\
  2668. See function `interrupt-process' for more details on usage.")
  2669.   (process, current_group)
  2670.      Lisp_Object process, current_group;
  2671. {
  2672. #ifndef SIGTSTP
  2673.   error ("no SIGTSTP support");
  2674. #else
  2675.   process_send_signal (process, SIGTSTP, current_group, 0);
  2676. #endif
  2677.   return process;
  2678. }
  2679.  
  2680. DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0,
  2681.   "Continue process PROCESS.  May be process or name of one.\n\
  2682. See function `interrupt-process' for more details on usage.")
  2683.   (process, current_group)
  2684.      Lisp_Object process, current_group;
  2685. {
  2686. #ifdef SIGCONT
  2687.     process_send_signal (process, SIGCONT, current_group, 0);
  2688. #else
  2689.     error ("no SIGCONT support");
  2690. #endif
  2691.   return process;
  2692. }
  2693.  
  2694. DEFUN ("signal-process", Fsignal_process, Ssignal_process,
  2695.   2, 2, "nProcess number: \nnSignal code: ",
  2696.   "Send the process with number PID the signal with code CODE.\n\
  2697. Both PID and CODE are integers.")
  2698.   (pid, sig)
  2699.      Lisp_Object pid, sig;
  2700. {
  2701.   CHECK_NUMBER (pid, 0);
  2702.   CHECK_NUMBER (sig, 1);
  2703. #ifdef WINDOWSNT
  2704.   /* Only works for kill-type signals */
  2705.   return make_number (Win32KillProcess (XINT (pid), XINT (sig)));
  2706. #else
  2707.   return make_number (kill (XINT (pid), XINT (sig)));
  2708. #endif
  2709. }
  2710.  
  2711. DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0,
  2712.   "Make PROCESS see end-of-file in its input.\n\
  2713. Eof comes after any text already sent to it.\n\
  2714. PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
  2715. nil, indicating the current buffer's process.")
  2716.   (process)
  2717.      Lisp_Object process;
  2718. {
  2719.   Lisp_Object proc;
  2720.  
  2721.   proc = get_process (process);
  2722.  
  2723.   /* Make sure the process is really alive.  */
  2724.   if (! NILP (XPROCESS (proc)->raw_status_low))
  2725.     update_status (XPROCESS (proc));
  2726.   if (! EQ (XPROCESS (proc)->status, Qrun))
  2727.     error ("Process %s not running", XSTRING (XPROCESS (proc)->name)->data);
  2728.  
  2729.   /* Sending a zero-length record is supposed to mean eof
  2730.      when TIOCREMOTE is turned on.  */
  2731. #ifdef DID_REMOTE
  2732.   {
  2733.     char buf[1];
  2734.     write (XFASTINT (XPROCESS (proc)->outfd), buf, 0);
  2735.   }
  2736. #else /* did not do TOICREMOTE */
  2737. #ifdef VMS
  2738.   send_process (proc, "\032", 1);     /* ^z */
  2739. #else
  2740.   if (!NILP (XPROCESS (proc)->pty_flag))
  2741.     send_process (proc, "\004", 1);
  2742.   else
  2743.     {
  2744.       close (XPROCESS (proc)->outfd);
  2745.       XFASTINT (XPROCESS (proc)->outfd) = open (NULL_DEVICE, O_WRONLY);
  2746.     }
  2747. #endif /* VMS */
  2748. #endif /* did not do TOICREMOTE */
  2749.   return process;
  2750. }
  2751.  
  2752. /* Kill all processes associated with `buffer'.
  2753.  If `buffer' is nil, kill all processes  */
  2754.  
  2755. _VOID_
  2756. kill_buffer_processes (buffer)
  2757.      Lisp_Object buffer;
  2758. {
  2759.   Lisp_Object tail, proc;
  2760.  
  2761.   for (tail = Vprocess_alist; XGCTYPE (tail) == Lisp_Cons;
  2762.        tail = XCONS (tail)->cdr)
  2763.     {
  2764.       proc = XCONS (XCONS (tail)->car)->cdr;
  2765.       if (XGCTYPE (proc) == Lisp_Process
  2766.       && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
  2767.     {
  2768.       if (NETCONN_P (proc))
  2769.         deactivate_process (proc);
  2770.       else if (XFASTINT (XPROCESS (proc)->infd))
  2771.         process_send_signal (proc, SIGHUP, Qnil, 1);
  2772.     }
  2773.     }
  2774. }
  2775.  
  2776. /* On receipt of a signal that a child status has changed,
  2777.  loop asking about children with changed statuses until
  2778.  the system says there are no more.
  2779.    All we do is change the status;
  2780.  we do not run sentinels or print notifications.
  2781.  That is saved for the next time keyboard input is done,
  2782.  in order to avoid timing errors.  */
  2783.  
  2784. /** WARNING: this can be called during garbage collection.
  2785.  Therefore, it must not be fooled by the presence of mark bits in
  2786.  Lisp objects.  */
  2787.  
  2788. /** USG WARNING:  Although it is not obvious from the documentation
  2789.  in signal(2), on a USG system the SIGCLD handler MUST NOT call
  2790.  signal() before executing at least one wait(), otherwise the handler
  2791.  will be called again, resulting in an infinite loop.  The relevant
  2792.  portion of the documentation reads "SIGCLD signals will be queued
  2793.  and the signal-catching function will be continually reentered until
  2794.  the queue is empty".  Invoking signal() causes the kernel to reexamine
  2795.  the SIGCLD queue.   Fred Fish, UniSoft Systems Inc. */
  2796.  
  2797. SIGTYPE _CALLBACK_
  2798. sigchld_handler (signo)
  2799.      int signo;
  2800. {
  2801.   int old_errno = errno;
  2802.   Lisp_Object proc;
  2803.   register struct Lisp_Process *p;
  2804.   extern EMACS_TIME *input_available_clear_time;
  2805.  
  2806. #ifdef BSD4_1
  2807.   extern int sigheld;
  2808.   sigheld |= sigbit (SIGCHLD);
  2809. #endif
  2810.  
  2811.   while (1)
  2812.     {
  2813.       register int pid;
  2814.       WAITTYPE w;
  2815.       Lisp_Object tail;
  2816.  
  2817. #ifdef WNOHANG
  2818. #ifndef WUNTRACED
  2819. #define WUNTRACED 0
  2820. #endif /* no WUNTRACED */
  2821.       /* Keep trying to get a status until we get a definitive result.  */
  2822.       do 
  2823.     {
  2824.       errno = 0;
  2825.       pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
  2826.     }
  2827.       while (pid <= 0 && errno == EINTR);
  2828.  
  2829.       if (pid <= 0)
  2830.     {
  2831.       /* A real failure.  We have done all our job, so return.  */
  2832.  
  2833.       /* USG systems forget handlers when they are used;
  2834.          must reestablish each time */
  2835. #ifdef USG
  2836.       signal (signo, sigchld_handler);   /* WARNING - must come after wait3() */
  2837. #endif
  2838. #ifdef  BSD4_1
  2839.       sigheld &= ~sigbit (SIGCHLD);
  2840.       sigrelse (SIGCHLD);
  2841. #endif
  2842.       errno = old_errno;
  2843.       return;
  2844.     }
  2845. #else
  2846.       pid = wait (&w);
  2847. #endif /* no WNOHANG */
  2848.  
  2849.       /* Find the process that signaled us, and record its status.  */
  2850.  
  2851.       p = 0;
  2852.       for (tail = Vprocess_alist; XSYMBOL (tail) != XSYMBOL (Qnil); tail = XCONS (tail)->cdr)
  2853.     {
  2854.       proc = XCONS (XCONS (tail)->car)->cdr;
  2855.       p = XPROCESS (proc);
  2856.       if (EQ (p->childp, Qt) && XFASTINT (p->pid) == pid)
  2857.         break;
  2858.       p = 0;
  2859.     }
  2860.  
  2861.       /* Look for an asynchronous process whose pid hasn't been filled
  2862.      in yet.  */
  2863.       if (p == 0)
  2864.     for (tail = Vprocess_alist; XSYMBOL (tail) != XSYMBOL (Qnil); tail = XCONS (tail)->cdr)
  2865.       {
  2866.         proc = XCONS (XCONS (tail)->car)->cdr;
  2867.         p = XPROCESS (proc);
  2868.         if (XTYPE (p->pid) == Lisp_Int && XINT (p->pid) == -1)
  2869.           break;
  2870.         p = 0;
  2871.       }
  2872.       
  2873.       /* Change the status of the process that was found.  */
  2874.       if (p != 0)
  2875.     {
  2876.       union { int i; WAITTYPE wt; } u;
  2877.       
  2878.       XSETINT (p->tick, ++process_tick);
  2879.       u.wt = w;
  2880.       XFASTINT (p->raw_status_low) = u.i & 0xffff;
  2881.       XFASTINT (p->raw_status_high) = u.i >> 16;
  2882.       
  2883.       /* If process has terminated, stop waiting for its output.  */
  2884.       if (WIFSIGNALED (w) || WIFEXITED (w))
  2885.         if (XFASTINT (p->infd))
  2886.           FD_CLR (XFASTINT (p->infd), &input_wait_mask);
  2887.  
  2888.       /* Tell wait_reading_process_input that it needs to wake up and
  2889.          look around.  */
  2890.       if (input_available_clear_time)
  2891.         EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
  2892.     }
  2893.  
  2894.     /* There was no asynchronous process found for that id.  Check
  2895.        if we have a synchronous process.  */
  2896.       else
  2897.     {
  2898.       synch_process_alive = 0;
  2899.  
  2900.       /* Report the status of the synchronous process.  */
  2901.       if (WIFEXITED (w))
  2902.         synch_process_retcode = WRETCODE (w);
  2903.       else if (WIFSIGNALED (w))
  2904. #ifndef VMS
  2905.         synch_process_death = (char *) sys_siglist[WTERMSIG (w)];
  2906. #else
  2907.         synch_process_death = sys_errlist[WTERMSIG (w)];
  2908. #endif
  2909.  
  2910.       /* Tell wait_reading_process_input that it needs to wake up and
  2911.          look around.  */
  2912.       if (input_available_clear_time)
  2913.         EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
  2914.     }
  2915.  
  2916.       /* On some systems, we must return right away.
  2917.      If any more processes want to signal us, we will
  2918.      get another signal.
  2919.      Otherwise (on systems that have WNOHANG), loop around
  2920.      to use up all the processes that have something to tell us.  */
  2921. #if (defined (USG) && ! (defined (HPUX) && defined (WNOHANG))) || \
  2922.       defined (WINDOWSNT)
  2923. #ifdef USG
  2924.       signal (signo, sigchld_handler);
  2925. #endif
  2926.       errno = old_errno;
  2927.       return;
  2928. #endif /* USG, but not HPUX with WNOHANG */
  2929.     }
  2930. }
  2931.  
  2932.  
  2933. static Lisp_Object
  2934. exec_sentinel_unwind (data)
  2935.      Lisp_Object data;
  2936. {
  2937.   XPROCESS (XCONS (data)->car)->sentinel = XCONS (data)->cdr;
  2938.   return Qnil;
  2939. }
  2940.  
  2941. static void
  2942. exec_sentinel (proc, reason)
  2943.      Lisp_Object proc, reason;
  2944. {
  2945.   Lisp_Object sentinel;
  2946.   register struct Lisp_Process *p = XPROCESS (proc);
  2947.   int count = specpdl_ptr - specpdl;
  2948.  
  2949.   sentinel = p->sentinel;
  2950.   if (NILP (sentinel))
  2951.     return;
  2952.  
  2953.   /* Zilch the sentinel while it's running, to avoid recursive invocations;
  2954.      assure that it gets restored no matter how the sentinel exits.  */
  2955.   p->sentinel = Qnil;
  2956.   record_unwind_protect (exec_sentinel_unwind, Fcons (proc, sentinel));
  2957.   /* Inhibit quit so that random quits don't screw up a running filter.  */
  2958.   specbind (Qinhibit_quit, Qt);
  2959.   call2 (sentinel, proc, reason);
  2960.   unbind_to (count, Qnil);
  2961. }
  2962.  
  2963. /* Report all recent events of a change in process status
  2964.    (either run the sentinel or output a message).
  2965.    This is done while Emacs is waiting for keyboard input.  */
  2966.  
  2967. _VOID_
  2968. status_notify ()
  2969. {
  2970.   register Lisp_Object proc, buffer;
  2971.   Lisp_Object tail = Qnil;
  2972.   Lisp_Object msg = Qnil;
  2973.   struct gcpro gcpro1, gcpro2;
  2974.  
  2975.   /* We need to gcpro tail; if read_process_output calls a filter
  2976.      which deletes a process and removes the cons to which tail points
  2977.      from Vprocess_alist, and then causes a GC, tail is an unprotected
  2978.      reference.  */
  2979.   GCPRO2 (tail, msg);
  2980.  
  2981.   for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
  2982.     {
  2983.       Lisp_Object symbol;
  2984.       register struct Lisp_Process *p;
  2985.  
  2986.       proc = Fcdr (Fcar (tail));
  2987.       p = XPROCESS (proc);
  2988.  
  2989.       if (XINT (p->tick) != XINT (p->update_tick))
  2990.     {
  2991.       XSETINT (p->update_tick, XINT (p->tick));
  2992.  
  2993.       /* If process is still active, read any output that remains.  */
  2994.       if (XFASTINT (p->infd))
  2995.         while (read_process_output (proc, XFASTINT (p->infd)) > 0);
  2996.  
  2997.       buffer = p->buffer;
  2998.  
  2999.       /* Get the text to use for the message.  */
  3000.       if (!NILP (p->raw_status_low))
  3001.         update_status (p);
  3002.       msg = status_message (p->status);
  3003.  
  3004.       /* If process is terminated, deactivate it or delete it.  */
  3005.       symbol = p->status;
  3006.       if (XTYPE (p->status) == Lisp_Cons)
  3007.         symbol = XCONS (p->status)->car;
  3008.  
  3009.       if (EQ (symbol, Qsignal) || EQ (symbol, Qexit)
  3010.           || EQ (symbol, Qclosed))
  3011.         {
  3012.           if (delete_exited_processes)
  3013.         remove_process (proc);
  3014.           else
  3015.         deactivate_process (proc);
  3016.         }
  3017.  
  3018.       /* Now output the message suitably.  */
  3019.       if (!NILP (p->sentinel))
  3020.         exec_sentinel (proc, msg);
  3021.       /* Don't bother with a message in the buffer
  3022.          when a process becomes runnable.  */
  3023.       else if (!EQ (symbol, Qrun) && !NILP (buffer))
  3024.         {
  3025.           Lisp_Object ro = XBUFFER (buffer)->read_only;
  3026.           Lisp_Object tem;
  3027.           struct buffer *old = current_buffer;
  3028.           int opoint;
  3029.  
  3030.           /* Avoid error if buffer is deleted
  3031.          (probably that's why the process is dead, too) */
  3032.           if (NILP (XBUFFER (buffer)->name))
  3033.         continue;
  3034.           Fset_buffer (buffer);
  3035.           opoint = point;
  3036.           /* Insert new output into buffer
  3037.          at the current end-of-output marker,
  3038.          thus preserving logical ordering of input and output.  */
  3039.           if (XMARKER (p->mark)->buffer)
  3040.         SET_PT (marker_position (p->mark));
  3041.           else
  3042.         SET_PT (ZV);
  3043.           if (point <= opoint)
  3044.         opoint += XSTRING (msg)->size + XSTRING (p->name)->size + 10;
  3045.  
  3046.           tem = current_buffer->read_only;
  3047.           current_buffer->read_only = Qnil;
  3048.           insert_string ("\nProcess ");
  3049.           Finsert (1, &p->name);
  3050.           insert_string (" ");
  3051.           Finsert (1, &msg);
  3052.           current_buffer->read_only = tem;
  3053.           Fset_marker (p->mark, make_number (point), p->buffer);
  3054.  
  3055.           SET_PT (opoint);
  3056.           set_buffer_internal (old);
  3057.         }
  3058.     }
  3059.     } /* end for */
  3060.  
  3061.   update_mode_lines++;  /* in case buffers use %s in mode-line-format */
  3062.   redisplay_preserve_echo_area ();
  3063.  
  3064.   update_tick = process_tick;
  3065.  
  3066.   UNGCPRO;
  3067. }
  3068.  
  3069. _VOID_
  3070. init_process ()
  3071. {
  3072.   register int i;
  3073.  
  3074. #ifdef SIGCHLD
  3075. #ifndef CANNOT_DUMP
  3076.   if (! noninteractive || initialized)
  3077. #endif
  3078.     signal (SIGCHLD, sigchld_handler);
  3079. #endif
  3080.  
  3081.   FD_ZERO (&input_wait_mask);
  3082.   FD_SET (0, &input_wait_mask);
  3083.   Vprocess_alist = Qnil;
  3084.   for (i = 0; i < MAXDESC; i++)
  3085.     {
  3086.       chan_process[i] = Qnil;
  3087.       proc_buffered_char[i] = -1;
  3088.     }
  3089. }
  3090. #if 0
  3091. DEFUN ("process-connection", Fprocess_connection, Sprocess_connection, 0, 1, 0,
  3092.  "Return the connection type of `PROCESS'.  This can be nil (pipe),\n\
  3093. t or pty (pty) or stream (socket connection).")
  3094.   (process)
  3095.      Lisp_Object process;
  3096. {
  3097.   return XPROCESS (process)->type;
  3098. }
  3099. #endif
  3100. _VOID_
  3101. syms_of_process ()
  3102. {
  3103. #ifdef HAVE_SOCKETS
  3104.   stream_process = intern ("stream");
  3105. #endif
  3106.   Qprocessp = intern ("processp");
  3107.   staticpro (&Qprocessp);
  3108.   Qrun = intern ("run");
  3109.   staticpro (&Qrun);
  3110.   Qstop = intern ("stop");
  3111.   staticpro (&Qstop);
  3112.   Qsignal = intern ("signal");
  3113.   staticpro (&Qsignal);
  3114.  
  3115.   /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
  3116.      here again.
  3117.  
  3118.      Qexit = intern ("exit");
  3119.      staticpro (&Qexit); */
  3120.  
  3121.   Qopen = intern ("open");
  3122.   staticpro (&Qopen);
  3123.   Qclosed = intern ("closed");
  3124.   staticpro (&Qclosed);
  3125.  
  3126.   staticpro (&Vprocess_alist);
  3127.  
  3128.   DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
  3129.     "*Non-nil means delete processes immediately when they exit.\n\
  3130. nil means don't delete them until `list-processes' is run.");
  3131.  
  3132.   delete_exited_processes = 1;
  3133.  
  3134.   DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type,
  3135.     "Control type of device used to communicate with subprocesses.\n\
  3136. Values are nil to use a pipe, and t or 'pty for a pty.  Note that if\n\
  3137. pty's are not available, this variable will be ignored. The value takes\n\
  3138. effect when `start-process' is called.");
  3139.   Vprocess_connection_type = Qt;
  3140.  
  3141.   defsubr (&Sprocessp);
  3142.   defsubr (&Sget_process);
  3143.   defsubr (&Sget_buffer_process);
  3144.   defsubr (&Sdelete_process);
  3145.   defsubr (&Sprocess_status);
  3146.   defsubr (&Sprocess_exit_status);
  3147.   defsubr (&Sprocess_id);
  3148.   defsubr (&Sprocess_name);
  3149.   defsubr (&Sprocess_command);
  3150.   defsubr (&Sset_process_buffer);
  3151.   defsubr (&Sprocess_buffer);
  3152.   defsubr (&Sprocess_mark);
  3153.   defsubr (&Sset_process_filter);
  3154.   defsubr (&Sprocess_filter);
  3155.   defsubr (&Sset_process_sentinel);
  3156.   defsubr (&Sprocess_sentinel);
  3157.   defsubr (&Sprocess_kill_without_query);
  3158.   defsubr (&Slist_processes);
  3159.   defsubr (&Sprocess_list);
  3160.   defsubr (&Sstart_process);
  3161. #ifdef HAVE_SOCKETS
  3162.   defsubr (&Sopen_network_stream);
  3163. #endif /* HAVE_SOCKETS */
  3164.   defsubr (&Saccept_process_output);
  3165.   defsubr (&Sprocess_send_region);
  3166.   defsubr (&Sprocess_send_string);
  3167.   defsubr (&Sinterrupt_process);
  3168.   defsubr (&Skill_process);
  3169.   defsubr (&Squit_process);
  3170.   defsubr (&Sstop_process);
  3171.   defsubr (&Scontinue_process);
  3172.   defsubr (&Sprocess_send_eof);
  3173.   defsubr (&Ssignal_process);
  3174.   defsubr (&Swaiting_for_user_input_p);
  3175. /*  defsubr (&Sprocess_connection); */
  3176. }
  3177.  
  3178.  
  3179. #else /* not subprocesses */
  3180.  
  3181. #include <sys/types.h>
  3182. #include <errno.h>
  3183.  
  3184. #include "lisp.h"
  3185. #include "systime.h"
  3186. #include "termopts.h"
  3187.  
  3188. extern int frame_garbaged;
  3189.  
  3190.  
  3191. /* As described above, except assuming that there are no subprocesses:
  3192.  
  3193.    Wait for timeout to elapse and/or keyboard input to be available.
  3194.  
  3195.    time_limit is:
  3196.      timeout in seconds, or
  3197.      zero for no limit, or
  3198.      -1 means gobble data immediately available but don't wait for any.
  3199.  
  3200.    read_kbd is a Lisp_Object:
  3201.      0 to ignore keyboard input, or
  3202.      1 to return when input is available, or
  3203.      -1 means caller will actually read the input, so don't throw to
  3204.        the quit handler.
  3205.      We know that read_kbd will never be a Lisp_Process, since
  3206.      `subprocesses' isn't defined.
  3207.  
  3208.    do_display != 0 means redisplay should be done to show subprocess
  3209.    output that arrives.  This version of the function ignores it.
  3210.  
  3211.    Return true iff we received input from any process.  */
  3212.  
  3213. int
  3214. wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
  3215.      int time_limit, microsecs;
  3216.      Lisp_Object read_kbd;
  3217.      int do_display;
  3218. {
  3219.   EMACS_TIME end_time, timeout, *timeout_p;
  3220.   int waitchannels;
  3221.  
  3222.   /* What does time_limit really mean?  */
  3223.   if (time_limit || microsecs)
  3224.     {
  3225.       /* It's not infinite.  */
  3226.       timeout_p = &timeout;
  3227.  
  3228.       if (time_limit == -1)
  3229.     /* In fact, it's zero.  */
  3230.     EMACS_SET_SECS_USECS (timeout, 0, 0);
  3231.       else
  3232.     EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
  3233.  
  3234.       /* How far in the future is that?  */
  3235.       EMACS_GET_TIME (end_time);
  3236.       EMACS_ADD_TIME (end_time, end_time, timeout);
  3237.     }
  3238.   else
  3239.     /* It's infinite.  */
  3240.     timeout_p = 0;
  3241.  
  3242.   /* Turn off periodic alarms (in case they are in use)
  3243.      because the select emulator uses alarms.  */
  3244.   stop_polling ();
  3245.  
  3246.   for (;;)
  3247.     {
  3248.       int nfds;
  3249.  
  3250.       waitchannels = XINT (read_kbd) ? 1 : 0;
  3251.  
  3252.       /* If calling from keyboard input, do not quit
  3253.      since we want to return C-g as an input character.
  3254.      Otherwise, do pending quit if requested.  */
  3255.       if (XINT (read_kbd) >= 0)
  3256.     QUIT;
  3257.  
  3258.       if (timeout_p)
  3259.     {
  3260.       EMACS_GET_TIME (*timeout_p);
  3261.       EMACS_SUB_TIME (*timeout_p, end_time, *timeout_p);
  3262.       if (EMACS_TIME_NEG_P (*timeout_p))
  3263.         break;
  3264.     }
  3265.  
  3266.       /* Cause C-g and alarm signals to take immediate action,
  3267.      and cause input available signals to zero out timeout.  */
  3268.       if (XINT (read_kbd) < 0)
  3269.     set_waiting_for_input (&timeout);
  3270.  
  3271.       /* If a frame has been newly mapped and needs updating,
  3272.      reprocess its display stuff.  */
  3273.       if (frame_garbaged)
  3274.     redisplay_preserve_echo_area ();
  3275.  
  3276.       if (XINT (read_kbd) && detect_input_pending ())
  3277.     nfds = 0;
  3278.       else
  3279.     nfds = select (1, &waitchannels, 0, 0, timeout_p);
  3280.  
  3281.       /* Make C-g and alarm signals set flags again */
  3282.       clear_waiting_for_input ();
  3283.  
  3284.       /*  If we woke up due to SIGWINCH, actually change size now.  */
  3285.       do_pending_window_change ();
  3286.  
  3287.       if (nfds == -1)
  3288.     {
  3289.       /* If the system call was interrupted, then go around the
  3290.          loop again.  */
  3291.       if (errno == EINTR)
  3292.         waitchannels = 0;
  3293.     }
  3294. #ifdef sun
  3295.       else if (nfds > 0 && (waitchannels & 1)  && interrupt_input)
  3296.     /* System sometimes fails to deliver SIGIO.  */
  3297.     kill (getpid (), SIGIO);
  3298. #endif
  3299. #ifdef SIGIO
  3300.       if (XINT (read_kbd) && interrupt_input && (waitchannels & 1))
  3301.     kill (0, SIGIO);
  3302. #endif
  3303.  
  3304.       /* If we have timed out (nfds == 0) or found some input (nfds > 0),
  3305.      we should exit.  */
  3306.       if (nfds >= 0)
  3307.     break;
  3308.     }
  3309.  
  3310.   start_polling ();
  3311.  
  3312.   return 0;
  3313. }
  3314.  
  3315.  
  3316. DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
  3317.   "Return the (or, a) process associated with BUFFER.\n\
  3318. This copy of Emacs has not been built to support subprocesses, so this\n\
  3319. function always returns nil.")
  3320.   (name)
  3321.      register Lisp_Object name;
  3322. {
  3323.   return Qnil;
  3324. }
  3325.  
  3326. /* Kill all processes associated with `buffer'.
  3327.    If `buffer' is nil, kill all processes.
  3328.    Since we have no subprocesses, this does nothing.  */
  3329.  
  3330. _VOID_
  3331. kill_buffer_processes (buffer)
  3332.      Lisp_Object buffer;
  3333. {
  3334. }
  3335.  
  3336. _VOID_
  3337. init_process ()
  3338. {
  3339. }
  3340.  
  3341. _VOID_
  3342. syms_of_process ()
  3343. {
  3344.   defsubr (&Sget_buffer_process);
  3345. }
  3346.  
  3347.  
  3348. #endif /* not subprocesses */
  3349.